Skip to content

Instantly share code, notes, and snippets.

@talhahasanzia
Created July 15, 2016 05:43
Show Gist options
  • Save talhahasanzia/6e3611ddc98abc197757bd9b9c073743 to your computer and use it in GitHub Desktop.
Save talhahasanzia/6e3611ddc98abc197757bd9b9c073743 to your computer and use it in GitHub Desktop.
Get best location from any of the location providers that are available.
private Location getLastKnownLocation() {
// get location from any of the location providers that are available
locMan = (LocationManager) getActivity().getSystemService(getActivity().LOCATION_SERVICE);
List<String> providers = locMan.getProviders(true);
Location bestLocation = null;
// check for all providers
for (String provider : providers) {
Location l = locMan.getLastKnownLocation(provider);
if (l == null) {
continue;
}
if (bestLocation == null || l.getAccuracy() < bestLocation.getAccuracy()) {
// Found best last known location: %s", l);
bestLocation = l;
}
}
return bestLocation;
}
Needs to have permissions on:
- FINE_LOCATION
- COURSE_LOCATION
- Hardware Location
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-feature android:name="android.hardware.location" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment