Advertisement
Guest User

android wrong way to do GPS location comparison

a guest
Jan 5th, 2011
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.20 KB | None | 0 0
  1. private static final int TWO_MINUTES = 1000 * 60 * 2;
  2.  
  3. /** Determines whether one Location reading is better than the current Location fix
  4.   * @param location  The new Location that you want to evaluate
  5.   * @param currentBestLocation  The current Location fix, to which you want to compare the new one
  6.   */
  7. protected boolean isBetterLocation(Location location, Location currentBestLocation) {
  8.     if (currentBestLocation == null) {
  9.         // A new location is always better than no location
  10.         return true;
  11.     }
  12.  
  13.     // Check whether the new location fix is newer or older
  14.     long timeDelta = location.getTime() - currentBestLocation.getTime();
  15.     boolean isSignificantlyNewer = timeDelta > TWO_MINUTES;
  16.     boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES;
  17.     boolean isNewer = timeDelta > 0;
  18.  
  19.     // If it's been more than two minutes since the current location, use the new location
  20.     // because the user has likely moved
  21.     if (isSignificantlyNewer) {
  22.         return true;
  23.     // If the new location is more than two minutes older, it must be worse
  24.     } else if (isSignificantlyOlder) {
  25.         return false;
  26.     }
  27.  
  28.     // Check whether the new location fix is more or less accurate
  29.     int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy());
  30.     boolean isLessAccurate = accuracyDelta > 0;
  31.     boolean isMoreAccurate = accuracyDelta < 0;
  32.     boolean isSignificantlyLessAccurate = accuracyDelta > 200;
  33.  
  34.     // Check if the old and new location are from the same provider
  35.     boolean isFromSameProvider = isSameProvider(location.getProvider(),
  36.             currentBestLocation.getProvider());
  37.  
  38.     // Determine location quality using a combination of timeliness and accuracy
  39.     if (isMoreAccurate) {
  40.         return true;
  41.     } else if (isNewer && !isLessAccurate) {
  42.         return true;
  43.     } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) {
  44.         return true;
  45.     }
  46.     return false;
  47. }
  48.  
  49. /** Checks whether two providers are the same */
  50. private boolean isSameProvider(String provider1, String provider2) {
  51.     if (provider1 == null) {
  52.       return provider2 == null;
  53.     }
  54.     return provider1.equals(provider2);
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement