Created
July 15, 2016 05:43
-
-
Save talhahasanzia/6e3611ddc98abc197757bd9b9c073743 to your computer and use it in GitHub Desktop.
Get best location from any of the location providers that are available.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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