Skip to content

Instantly share code, notes, and snippets.

@j-onathan
Created August 6, 2014 17:03
Show Gist options
  • Save j-onathan/adc7d200cd5af2aa56be to your computer and use it in GitHub Desktop.
Save j-onathan/adc7d200cd5af2aa56be to your computer and use it in GitHub Desktop.
public static Location getLatestLocation(final Context context) {
LocationManager manager = (LocationManager) context
.getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(ACCURACY_FINE);
String provider = manager.getBestProvider(criteria, true);
Location bestLocation;
if (provider != null)
bestLocation = manager.getLastKnownLocation(provider);
else
bestLocation = null;
Location latestLocation = getLatest(bestLocation,
manager.getLastKnownLocation(GPS_PROVIDER));
latestLocation = getLatest(latestLocation,
manager.getLastKnownLocation(NETWORK_PROVIDER));
latestLocation = getLatest(latestLocation,
manager.getLastKnownLocation(PASSIVE_PROVIDER));
return latestLocation;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment