Created
August 6, 2014 17:03
-
-
Save j-onathan/adc7d200cd5af2aa56be to your computer and use it in GitHub Desktop.
Android Code Example: Finding current location (from https://www.codota.com/android/scenarios/52fcbdd6da0a6fdfa4630520/finding-current-location?tag=dragonfly)
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
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