Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
201 changes: 168 additions & 33 deletions app/src/main/java/com/cunycodes/bikearound/MapsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public class MapsActivity extends FragmentActivity implements OnMapReadyCallbac
private GoogleApiClient mGoogleApiClient;
private final int PERMISSION_LOCATION = 111;

StationInformation stationInformation = new StationInformation();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down Expand Up @@ -105,12 +107,11 @@ public void onMapReady(GoogleMap googleMap) {



//downloadCitiLocationsData();
mMap.setMyLocationEnabled(true);

}

public void onSearch(View view) {
public void onSearch(View view) throws JSONException {
EditText address = (EditText) findViewById(R.id.textAddress);
String location = address.getText().toString();
List<Address> addressList = null;
Expand All @@ -126,45 +127,44 @@ public void onSearch(View view) {

Address address1 = addressList.get(0);
LatLng latLng = new LatLng(address1.getLatitude(), address1.getLongitude());
mMap.addMarker(new MarkerOptions().position(latLng).title("Destination"));
int destID = stationInformation.getNearestLocationID(latLng);
LatLng destination = stationInformation.getLatLng(destID);
int bikeQty = stationInformation.getBikeQuantity(destID);
mMap.addMarker(new MarkerOptions().position(destination).title(stationInformation.getName(destID)).snippet(String.valueOf(bikeQty) + " bikes available"));
mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
Log.d("NEARESTSTATION", String.valueOf(stationInformation.getName(destID)));
}
}

public void downloadCitiLocationsData() {

final JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.GET, CITI_API_URL, null, new Response.Listener<JSONObject>() {

@Override
public void onResponse(JSONObject response) {

try {
//System.out.println(response.toString());
//Log.v("TEST_API_RESPONSE", "ERR: " + response.toString());

JSONObject data = response.getJSONObject("data");
JSONArray list = data.getJSONArray("stations");
int lastUpdate = response.getInt("last_updated");
long currentTimeSecs = System.currentTimeMillis() / 1000;

//System.out.print("THE LIST = " + list.toString());
int timeElapsedUpdate = (int) (currentTimeSecs - lastUpdate);

// JSONObject obj = list.getJSONObject(0);
// String lat = obj.getString("lat");
// System.out.println(lat);
// 40.76727216
stationInformation.setStationLocationList(list); //Add a JSONArray to class StationInformation for easy retrieval
Log.d("BIKE72", String.valueOf(stationInformation.getBikeQuantity(522)));

for(int i = 0; i < list.length(); i++) {
JSONObject obj = list.getJSONObject(i);
double lat = obj.getDouble("lat");
double lon = obj.getDouble("lon");
String name = obj.getString("name");
// System.out.println(lat);
// System.out.println(lon);


int stationID = obj.getInt("station_id");
Log.d("STATION_ID", String.valueOf(stationID));
LatLng latLng = new LatLng(lat, lon);
mMap.addMarker(new MarkerOptions().position(latLng).title(name));
mMap.addMarker(new MarkerOptions().position(latLng).title(name).snippet(getTimeSinceUpdateString(timeElapsedUpdate)));

}


} catch (JSONException e) {
Log.v("TEST_API_RESPONSE", "ERR: " );
Expand All @@ -182,31 +182,28 @@ public void onErrorResponse(VolleyError error) {
Volley.newRequestQueue(this).add(jsonRequest);
}

public JSONArray downloadCitiBikeQuantity()
{
JSONArray JSONStations = null; //declare this, return an array
final JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.GET, STATION_STATUS_URL, null, new Response.Listener<JSONObject>() {
/// fetches station status, returns information to class StationInformation
public void downloadCitiStatusData() {

final JsonObjectRequest jsonRequestStatus = new JsonObjectRequest(Request.Method.GET, STATION_STATUS_URL, null, new Response.Listener<JSONObject>() {

@Override
public void onResponse(JSONObject response) {

try {

JSONObject data = response.getJSONObject("data");
JSONArray list = data.getJSONArray("stations");
//Log.d("MAPSACTIVITY", list.toString());
//JSONStations = list;
// Log.d("MAPSACTIVITY", JSONStations.toString());
}
int lastUpdate = response.getInt("last_updated");

stationInformation.setStationUpdate(lastUpdate);
stationInformation.setStationStatusList(list);

catch (JSONException e) {
} catch (JSONException e) {
Log.v("TEST_API_RESPONSE", "ERR: " );
}

}



}, new Response.ErrorListener() {

@Override
Expand All @@ -215,14 +212,13 @@ public void onErrorResponse(VolleyError error) {
}
});

Volley.newRequestQueue(this).add(jsonRequest);
return JSONStations;
Volley.newRequestQueue(this).add(jsonRequestStatus);
}


@Override
public void onLocationChanged(Location location) {

Log.d("LOCATION", String.valueOf(location));
}

@Override
Expand Down Expand Up @@ -255,6 +251,25 @@ public void startLocationServices() {
}
}

public String getTimeSinceUpdateString(int timeElapsedUpdate){
String updateTimeString = "";
if (timeElapsedUpdate == 0)
{
updateTimeString = "now.";

}
else if (timeElapsedUpdate < 60)
{
updateTimeString = " < 1 min ago.";
}
else
{
updateTimeString = String.valueOf((int) timeElapsedUpdate / 60) + "minutes ago.";
}

return updateTimeString;

}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
Expand All @@ -272,15 +287,135 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
}
}

public class StationInformation{
private JSONArray stationStatusList;
private JSONArray stationLocationList;
private long updateTime;

public void setStationStatusList(JSONArray arr){
stationStatusList = arr;
Log.d("SETSTATUSLIST", "STATUSLIST ARRAY SET");

}

public void setStationUpdate(long time){
updateTime = time;
}

public void setStationLocationList(JSONArray arr){
stationLocationList = arr;
Log.d("SETLOCATIONLIST", "LOCATIONLIST ARRAY SET");
}

public int getBikeQuantity(int stationID) throws JSONException {
try {
for (int i = 0; i < stationStatusList.length(); i++) {
JSONObject obj = stationStatusList.getJSONObject(i);
int currentID = obj.getInt("station_id");
int bikeQuantity = obj.getInt("num_bikes_available");

if (currentID == stationID){
return bikeQuantity;
}
}

}
catch (JSONException e){
Log.e("JSONEXCEPTION", "ERROR FINDING QUANTITY OF BIKES");
}
Log.e("GETBIKEQUANTITY", "Couldnt find information from JSON, problem with ID?");
return -1;
}

public int getNearestLocationID(LatLng latLng){
try {
int nearestID = -1;
double nearestDistance = 9999;
for (int i = 0; i < stationLocationList.length(); i++) {
JSONObject obj = stationLocationList.getJSONObject(i);
double stationLat = obj.getDouble("lat");
double stationLng = obj.getDouble("lon");
int ID = obj.getInt("station_id");
double myLat = latLng.latitude;
double myLng = latLng.longitude;
double distance = Math.sqrt((Math.pow((stationLat - myLat), 2)) + (Math.pow((stationLng - myLng), 2)));
Log.d("DISTANCE", String.valueOf(distance));
if (distance < nearestDistance ){
nearestDistance = distance;
nearestID = ID;
}
Log.d("NEARESTDISTANCE", String.valueOf(nearestDistance));

}

return nearestID;
}
catch (JSONException e){
Log.e("JSONEXCEPTION", "ERROR FINDING NEAREST LOCATION");
}
return -1;

}

public LatLng getLatLng(int stationID){
double lat = -1;
double lng = -1;

try {
for (int i = 0; i < stationLocationList.length(); i++) {
JSONObject obj = stationLocationList.getJSONObject(i);
int currentID = obj.getInt("station_id");

if (currentID == stationID){
lat = obj.getDouble("lat");
lng = obj.getDouble("lon");
}

}
LatLng coordinates = new LatLng(lat, lng);
Log.d("LATLNG OF GIVEN ID", String.valueOf(coordinates));
return coordinates;
}
catch (JSONException e){
Log.e("JSONEXCEPTION", "ERROR FINDING LATLNG STATION FROM ID");
}
LatLng coordinates = new LatLng(-1,-1);
return coordinates;

}

public String getName(int stationID){
String name = "name";
try {
for (int i = 0; i < stationLocationList.length(); i++) {
JSONObject obj = stationLocationList.getJSONObject(i);
int currentID = obj.getInt("station_id");
if (currentID == stationID){
name = obj.getString("name");
}
}
}
catch (JSONException e){
Log.e("JSONEXCEPTION", "ERROR FINDING LATLNG STATION FROM ID");
}
return name;
}



}

private class FetchLocations extends AsyncTask<Void, Void, Void> {

@Override
protected Void doInBackground(Void... params) {
downloadCitiStatusData();
downloadCitiLocationsData();
return null;
}
}


}