从 LocationServices 返回的位置年龄
Age of location returned from LocationServices
我需要确保我使用的位置是新鲜的:
有没有办法找出 LocationServices.FusedLocationApi.getLastLocation
返回的定位结果的年代?
如果不是:如果我向 LocationServices.FusedLocationApi.requestLocationUpdates
(使用 setNumUpdates(1)
和 setMaxWaitTime(0)
)注册一个位置侦听器,那么如果位置与LocationServices.FusedLocationApi.getLastLocation
?
是的,有一个非常简单的方法。您可以像这样调用 getTime()
来获取位置修复的时间:
Location currentLocation = LocationServices.FusedLocationApi.getLastLocation(apiClient);
long locationAge = System.currentTimeMillis() - currentLocation.getTime();
if (locationAge <= 60 * 1000) { // not older than 60 seconds
// do something with the location
}
文档建议不要使用 System.currentTimeMillis() 进行时间比较,但我从未遇到过这种方法的任何缺陷。但是,您应该考虑阅读(简短的)文档:
https://developer.android.com/reference/android/location/Location.html#getTime()
为了扩展 Illiminat 的回答,从 API 17 开始,添加了 getElapsedRealtimeNanos()
方法。从方法的文档...
Return the time of this fix, in elapsed real-time since system boot.
This value can be reliably compared to SystemClock.elapsedRealtimeNanos(), to calculate the age of a fix and to compare Location fixes. This is reliable because elapsed real-time is guaranteed monotonic for each system boot and continues to increment even when the system is in deep sleep (unlike getTime().
https://developer.android.com/reference/android/location/Location.html#getElapsedRealtimeNanos()
因此,以下应该是最精确的计算方法
Location currentLocation = LocationServices.FusedLocationApi.getLastLocation(apiClient);
long locationAge = SystemClock.elapsedRealtimeNanos() - currentLocation.getElapsedRealtimeNanos();
long ageLimitNanoSec = 60_000_000_000; // 60 seconds in nano seconds
if (locationAge <= ageLimitNanoSec) {
// do something with the location
}
我需要确保我使用的位置是新鲜的:
有没有办法找出 LocationServices.FusedLocationApi.getLastLocation
返回的定位结果的年代?
如果不是:如果我向 LocationServices.FusedLocationApi.requestLocationUpdates
(使用 setNumUpdates(1)
和 setMaxWaitTime(0)
)注册一个位置侦听器,那么如果位置与LocationServices.FusedLocationApi.getLastLocation
?
是的,有一个非常简单的方法。您可以像这样调用 getTime()
来获取位置修复的时间:
Location currentLocation = LocationServices.FusedLocationApi.getLastLocation(apiClient);
long locationAge = System.currentTimeMillis() - currentLocation.getTime();
if (locationAge <= 60 * 1000) { // not older than 60 seconds
// do something with the location
}
文档建议不要使用 System.currentTimeMillis() 进行时间比较,但我从未遇到过这种方法的任何缺陷。但是,您应该考虑阅读(简短的)文档:
https://developer.android.com/reference/android/location/Location.html#getTime()
为了扩展 Illiminat 的回答,从 API 17 开始,添加了 getElapsedRealtimeNanos()
方法。从方法的文档...
Return the time of this fix, in elapsed real-time since system boot.
This value can be reliably compared to SystemClock.elapsedRealtimeNanos(), to calculate the age of a fix and to compare Location fixes. This is reliable because elapsed real-time is guaranteed monotonic for each system boot and continues to increment even when the system is in deep sleep (unlike getTime().
https://developer.android.com/reference/android/location/Location.html#getElapsedRealtimeNanos()
因此,以下应该是最精确的计算方法
Location currentLocation = LocationServices.FusedLocationApi.getLastLocation(apiClient);
long locationAge = SystemClock.elapsedRealtimeNanos() - currentLocation.getElapsedRealtimeNanos();
long ageLimitNanoSec = 60_000_000_000; // 60 seconds in nano seconds
if (locationAge <= ageLimitNanoSec) {
// do something with the location
}