启用 GPS 后,需要一些时间才能准备好 GPS.所以使用这个代码可能会得到一个过时的位置:位置 = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);我无法使用已过期的位置.如何获得我需要的位置?
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?
这是一个类似的问题:Android:getLastKnownLocation 过期 - 如何强制位置刷新?
从中,我可以使用 LocationListener 获取更新的位置,该位置应该不会过时.这是正确的.但是我该如何处理这种情况:
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:
我已经启用了 GPS,然后启动我的应用程序.如果我呆在一个地方不动,那么 onLocationChanged() 将不会被调用.所以不能用这个方法来判断getLastKnownLocation()的返回值是不是过期了?
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?
有什么想法吗?
如何获取未过期的位置?
How to get location that isn't out of date?
您调用 requestLocationUpdates()
的 minTime 为 1000 毫秒,对于 onLocationChanged()
中的每个 Location
,您调用 getAccuracy()代码>.如果您可以接受准确度,请调用
removeUpdates()
.
You call requestLocationUpdates()
with the minTime of 1000 ms, for every Location
in onLocationChanged()
, you call getAccuracy()
. If the accuracy is acceptable for you, call removeUpdates()
.
所以我不能用这个方法来判断getLastKnownLocation()的返回值是否过时
So I can't use this method to judge whether the return value of getLastKnownLocation() is not out-of-date
我认为它返回的 Location 对象将包含一个 时间戳.您可以为现在与位置的 getTime()
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()
这篇关于如何获取未过期的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!