確定啟動監聽位置更新的時機
你可能想要在應用程式一啟動就要監聽位置更新,或者是在使用者啟用某個特定功能之後才開始監聽位置更新。要注意的時,長時間的監聽位置的變化會消耗大量的電池電量,但是短期的監聽可能不會提供足夠的精度。
如上訴Demo,通過調用requestLocationUpdates()方法能夠開始監聽位置的更新:
LocationProvider locationProvider = LocationManager.NETWORK_PROVIDER;// Or, use GPS location data:// LocationProvider locationProvider = LocationManager.GPS_PROVIDER;locationManager.requestLocationUpdates(locationProvider, 0, 0, locationListener);
擷取快速修正的最後確定的位置 你
你的位置監聽器接受到第一個位置所要花費的時間經常要使用者等待很長時間,你應該通過調用getLastKnownLocation(String)方法來利用被緩衝的位置,直到有更精確的位置提供給你的位置監聽器:
LocationProvider locationProvider = LocationManager.NETWORK_PROVIDER;// Or use LocationManager.GPS_PROVIDERLocation lastKnownLocation = locationManager.getLastKnownLocation(locationProvider);
確定終止監聽位置更新的時機
決定不再需要新位置時機的邏輯,根據你的應用程式的需要,它的範圍從很簡單到很複雜。在擷取位置和使用該位置的短暫間隙,來改善估算的精度。始終要注意長時間的監聽要消耗大量的電池電量,一旦擷取你所需要的資訊,就應該通過調用removeUpdates(PendingIntent)方法來終止監聽位置的更新:
// Remove the listener you previously addedlocationManager.removeUpdates(locationListener);
保持當前的最佳估算值
你可能期望最近的位置修正是最精確的。但是,因為位置變化修正的精度,不總是最近的就是最好的。你應該包括基於幾種標準的選擇位置修正的邏輯。這些標準還要根據應用程式所使用的情境和地區來進行測試。
以下是驗證位置修正的精度的幾個步驟:
1.檢測新擷取的位置是否比之前的更新;
2.檢測新擷取的位置是否比之前的更精確或更差;
3.檢查新的位置是來源於哪個提供器,並判斷它是否更加真實可信。
以下樣本詳細說明了這些檢查的邏輯:
private static final int TWO_MINUTES = 1000 * 60 * 2;/** 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(), 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;}/** Checks whether two providers are the same */private boolean isSameProvider(String provider1, String provider2) { if (provider1 == null) { return provider2 == null; } return provider1.equals(provider2);}