Skip to content

Instantly share code, notes, and snippets.

@mherod
Created July 17, 2013 14:37
Show Gist options
  • Save mherod/6021164 to your computer and use it in GitHub Desktop.
Save mherod/6021164 to your computer and use it in GitHub Desktop.
LiveLocationManager for acquiring and optionally maintaining the most accurate and relevant location fix
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
public class LiveLocationManager implements LocationListener {
private final String TAG = "LiveLocationManager";
private LocationManager locationManager = null;
private Location bestCurrentLocation = null;
private static final int TWO_MINUTES = 1000 * 60 * 2;
public LiveLocationManager(Context context) {
locationManager = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
}
public void onLocationChanged(Location loc) {
if (isBetterLocation(loc, bestCurrentLocation)) {
bestCurrentLocation = loc;
Log.d(TAG, loc.getLatitude() + " : " + loc.getLongitude() + " : "
+ loc.getAccuracy() + " from " + loc.getProvider());
}
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
public void onProviderEnabled(String provider) {
}
public void onProviderDisabled(String provider) {
}
public Location getCurrentLocation() {
Location lastGPSLocation = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Location lastNetworkLocation = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (lastGPSLocation != null
&& isBetterLocation(lastGPSLocation, bestCurrentLocation)) {
bestCurrentLocation = lastGPSLocation;
}
if (lastNetworkLocation != null
&& isBetterLocation(lastNetworkLocation, bestCurrentLocation)) {
bestCurrentLocation = lastNetworkLocation;
}
return bestCurrentLocation;
}
protected Runnable startListeners = new Runnable() {
@Override
public void run() {
LocationListener handle = LiveLocationManager.this;
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0, handle);
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 0, 0, handle);
}
};
protected Runnable stopListeners = new Runnable() {
@Override
public void run() {
LocationListener handle = LiveLocationManager.this;
locationManager.removeUpdates(handle);
}
};
protected boolean isBetterLocation(Location location,
Location currentBestLocation) {
if (currentBestLocation == null) {
return true;
}
long timeDelta = location.getTime() - currentBestLocation.getTime();
boolean isSignificantlyNewer = timeDelta > TWO_MINUTES;
boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES;
boolean isNewer = timeDelta > 0;
if (isSignificantlyNewer) {
return true;
} else if (isSignificantlyOlder) {
return false;
}
int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation
.getAccuracy());
boolean isLessAccurate = accuracyDelta > 0;
boolean isMoreAccurate = accuracyDelta < 0;
boolean isSignificantlyLessAccurate = accuracyDelta > 200;
boolean isFromSameProvider = isSameProvider(location.getProvider(),
currentBestLocation.getProvider());
if (isMoreAccurate) {
return true;
} else if (isNewer && !isLessAccurate) {
return true;
} else if (isNewer && !isSignificantlyLessAccurate
&& isFromSameProvider) {
return true;
}
return false;
}
private boolean isSameProvider(String provider1, String provider2) {
if (provider1 == null) {
return provider2 == null;
}
return provider1.equals(provider2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment