How to get location that isn't out of date?

为君一笑 提交于 2019-12-02 13:36:37

问题


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?


回答1:


How to get location that isn't out of date?

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().

So I can't use this method to judge whether the return value of getLastKnownLocation() is not out-of-date

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()




回答2:


Each Location object has a getTime() method, which returns the time of the fix. You could use that to determine how old the fix is.



来源:https://stackoverflow.com/questions/7898930/how-to-get-location-that-isnt-out-of-date

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!