Skip to content

Commit

Permalink
Check for null gps before adding
Browse files Browse the repository at this point in the history
  • Loading branch information
schollz committed May 3, 2018
1 parent 761c009 commit 9678b21
Showing 1 changed file with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -302,10 +302,12 @@ public void sendData() {
if (allowGPS) {
JSONObject gps = new JSONObject();
Location loc = getLastBestLocation();
gps.put("lat",loc.getLatitude());
gps.put("lon",loc.getLongitude());
gps.put("alt",loc.getAltitude());
jsonBody.put("gps",gps);
if (loc != null) {
gps.put("lat",loc.getLatitude());
gps.put("lon",loc.getLongitude());
gps.put("alt",loc.getAltitude());
jsonBody.put("gps",gps);
}
}

final String mRequestBody = jsonBody.toString();
Expand Down Expand Up @@ -359,9 +361,11 @@ protected Response<String> parseNetworkResponse(NetworkResponse response) {
private Location getLastBestLocation() {
LocationManager mLocationManager = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);

Location locationGPS = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Location locationNet = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);


long GPSLocationTime = 0;
if (null != locationGPS) {
GPSLocationTime = locationGPS.getTime();
Expand All @@ -374,8 +378,10 @@ private Location getLastBestLocation() {
}

if (0 < GPSLocationTime - NetLocationTime) {
Log.d("GPS",locationGPS.toString());
return locationGPS;
} else {
Log.d("GPS",locationNet.toString());
return locationNet;
}
}
Expand Down

0 comments on commit 9678b21

Please sign in to comment.