privatestaticfinalint CHECK_INTERVAL =1000*30;protectedboolean isBetterLocation(Location location,
Location currentBestLocation) {
if (currentBestLocation ==null) {
// A new location is always better than no locationreturntrue;
}
// Check whether the new location fix is newer or olderlong timeDelta = location.getTime() - currentBestLocation.getTime();boolean isSignificantlyNewer = timeDelta > CHECK_INTERVAL;boolean isSignificantlyOlder = timeDelta <-CHECK_INTERVAL;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 movedif (isSignificantlyNewer) {
returntrue;// If the new location is more than two minutes older, it must// be worse
} elseif (isSignificantlyOlder) {
returnfalse;
}
// Check whether the new location fix is more or less accurateint 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 providerboolean isFromSameProvider = isSameProvider(location.getProvider(),
currentBestLocation.getProvider());// Determine location quality using a combination of timeliness and// accuracyif (isMoreAccurate) {
returntrue;
} elseif (isNewer &&!isLessAccurate) {
returntrue;
} elseif (isNewer &&!isSignificantlyLessAccurate
&& isFromSameProvider) {
returntrue;
}
returnfalse;
}
/** Checks whether two providers are the same */privateboolean isSameProvider(String provider1, String provider2) {
if (provider1 ==null) {
return provider2 ==null;
}
return provider1.equals(provider2);
}