如何获取未过时的位置?
启用 GPS 后,GPS 需要一段时间才能准备就绪。因此,使用此代码可能会获得已过时的位置: 位置 = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 我无法使用已过时的位置。我怎样才能找到我需要的位置?
这是一个类似的问题: Android:getLastKnownLocation 已过时 - 如何强制位置刷新?
从中,我可以使用 LocationListener 来获取更新的位置,该位置应该不会过时。这是正确的。但我该如何处理这种情况:
我已经启用了 GPS,然后启动我的应用程序。如果我停留在一个地方并且不移动,那么 onLocationChanged() 将不会被调用。所以我不能用这个方法来判断getLastKnownLocation()的返回值是否没有过期?
有什么想法吗?
After GPS is enabled, it will take some time before the GPS is ready. So use this code may get a location that is out of date:
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
I can't use the location that is out of date. How can I get the location that I need?
Here is a similar question:
Android: getLastKnownLocation out-of-date - how to force location refresh?
From it, I can use LocationListener to get the updated location, which should be not out of date. It's correct. But how can I handle this case:
I have already enabled GPS, and then start my app. If I stay at one place and don't move, then onLocationChanged() won't be called. So I can't use this method to judge whether the return value of getLastKnownLocation() is not out-of-date?
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您以 1000 毫秒的 minTime 调用
requestLocationUpdates()
,对于onLocationChanged()
中的每个Location
,您调用getAccuracy()< /代码>。如果您可以接受准确性,请调用
removeUpdates()
。我认为它返回的 Location 对象将包含一个 时间戳。您可以设置现在与位置
getTime()
之间的时差容差You call
requestLocationUpdates()
with the minTime of 1000 ms, for everyLocation
inonLocationChanged()
, you callgetAccuracy()
. If the accuracy is acceptable for you, callremoveUpdates()
.I think the Location object that it returns will contain a time stamp. You can set a tolerance for the time difference between now and the Location's
getTime()
每个
Location
对象都有一个getTime()
方法,该方法返回修复时间。您可以使用它来确定修复的时间。Each
Location
object has agetTime()
method, which returns the time of the fix. You could use that to determine how old the fix is.