portfoliobuilder portfoliobuilder - 10 months ago 38
Android Question

Is it bad practice to getLastKnownLocation any time someone moves a pin on the map?

I have implemented GoogleMap.OnMyLocationChangeListener and attached to my map object OnMyLocationChangeListener so that whenever any movement is done on the map, I can receive feedback of these changes in the callback method

public void onMyLocationChange(android.location.Location location) {

I was reading the Android Developers strategies documentation (https://developer.android.com/guide/topics/location/strategies.html) and learned that there is a function to determine whether one location reading is better than the current location fix

What I decided to do was check for a better location with every map change. In order to do this, I am passing in the lastKnownGPSLocation and the location object from onMyLocationChange callback. From my knowledge, calling getLastKnownLocation can be expensive and battery draining. This is the idea of what I am doing.

if (isBetterLocation(mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER), location) {
// update my current lat/long

And the function for isBetterLocation() is the following:

/** Determines whether one Location reading is better than the current Location fix
* @param location The new Location that you want to evaluate
* @param currentBestLocation The current Location fix, to which you want to compare the new one
protected boolean isBetterLocation(Location location, Location currentBestLocation) {
if (currentBestLocation == null) {
// A new location is always better than no location
return true;

// Check whether the new location fix is newer or older
long timeDelta = location.getTime() - currentBestLocation.getTime();
boolean isSignificantlyNewer = timeDelta > TWO_MINUTES;
boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES;
boolean isNewer = timeDelta > 0;

// If it's been more than two minutes since the current location, use the new location
// because the user has likely moved
if (isSignificantlyNewer) {
return true;
// If the new location is more than two minutes older, it must be worse
} else if (isSignificantlyOlder) {
return false;

// Check whether the new location fix is more or less accurate
int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy());
boolean isLessAccurate = accuracyDelta > 0;
boolean isMoreAccurate = accuracyDelta < 0;
boolean isSignificantlyLessAccurate = accuracyDelta > 200;

// Check if the old and new location are from the same provider
boolean isFromSameProvider = isSameProvider(location.getProvider(),

// Determine location quality using a combination of timeliness and accuracy
if (isMoreAccurate) {
return true;
} else if (isNewer && !isLessAccurate) {
return true;
} else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) {
return true;
return false;

/** Checks whether two providers are the same */
private boolean isSameProvider(String provider1, String provider2) {
if (provider1 == null) {
return provider2 == null;
return provider1.equals(provider2);

Is this how this function is intended to be used? I have noticed that other people use this as a Service. Thanks in advance.


According to this documentation, in working with location data, the location data available to an Android device includes the current location of the device pinpointed using a combination of technologies. The direction and method of movement, and whether the device has moved across a predefined geographical boundary, or geofence. Then depending on the needs of your application, you can choose between these methods in working with location data.

  • The My Location layer provides a simple way to display a device's location on the map. It does not provide data.

  • The Google Play services Location API is recommended for all programmatic requests for location data.

  • The LocationSource interface allows you to provide a custom location provider.

Also, from this documentation, the Google Play services location APIs are now the preferred way in adding a location awareness in the app. Just read the documentation link above to know more information and sample code on how to achieve this.