Android: getLastKnownLocation out-of-date - how to force location refresh?

こ雲淡風輕ζ 提交于 2019-11-27 14:58:25

问题


I'm using

myLocation = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

to retrieve the current location at the start-up of my application.

As the Android documentation states, this location can be "out-of-date", since the method returns the location when the GPS was used the last time.

How can I actively request the current location from the GPS? I thought about using LocationListener, however that might be a bit of an overkill, since I only need the location once (at the start of my app).

Any ideas?


回答1:


Your initial intuition is correct - you need to use a LocationListener to request updates. Given that you require only a single position, you can unregister the LocationListener after the first value returns.

In practice though, it's probably wise to listen for a little bit longer. Location Based Services (particularly GPS) have a tendency to 'jump around' a bit when they first get their fix. Your best bet is to listen for a set amount of time, or a set number of updates, or until a certain level of accuracy has been achieved (the Location Listener will return the accuracy of the position returned).



来源:https://stackoverflow.com/questions/2056163/android-getlastknownlocation-out-of-date-how-to-force-location-refresh

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