简体   繁体   中英

Android: Developer site guidelines on getting better location fix

This is the code snippet that the developer site has provided for getting a better fix. I have a doubt in this. In this implementation, the function checks if it is significantly newer, then it returns true in the very beginning itself. But what if the newer fix is from the network provider and if it is less accurate. Still it returns true, which should not be the case. Is this a bug or is my understanding wrong ?

Link: http://developer.android.com/guide/topics/location/obtaining-user-location.html

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(),
        currentBestLocation.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;

}

Significantly newer means location has changed a lot and thus even a network location fix is more precise than an older gps location fix. So yes that makes sense.

Nevertheless, I think your idea to give priority to the finest location source is good, you should implement it and maybe maintain two different fixes from the two different sources.

It would have been nice to provide a link to the tutorial you got inspired by.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM