Skip to content

Instantly share code, notes, and snippets.

@100rabhkr
Created August 4, 2018 06:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 100rabhkr/7c9590292d752418776c3e608d09ff0c to your computer and use it in GitHub Desktop.
Save 100rabhkr/7c9590292d752418776c3e608d09ff0c to your computer and use it in GitHub Desktop.
LocationManager mLocationManager;
Location myLocation = getLastKnownLocation();
private Location getLastKnownLocation() {
mLocationManager = (LocationManager)getApplicationContext().getSystemService(LOCATION_SERVICE);
List<String> providers = mLocationManager.getProviders(true);
Location bestLocation = null;
for (String provider : providers) {
Location l = mLocationManager.getLastKnownLocation(provider);
if (l == null) {
continue;
}
if (bestLocation == null || l.getAccuracy() < bestLocation.getAccuracy()) {
// Found best last known location: %s", l);
bestLocation = l;
}
}
return bestLocation;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment