問題描述
我正在使用
myLocation = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
在我的應用程序啟動時檢索當前位置.
to retrieve the current location at the start-up of my application.
正如 Android 文檔所述,此位置可能過時",因為該方法返回上次使用 GPS 時的位置.
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.
我如何主動從 GPS 請求當前位置?我考慮過使用 LocationListener
,但這可能有點矯枉過正,因為我只需要一次位置(在我的應用程序開始時).
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).
有什么想法嗎?
推薦答案
你最初的直覺是正確的——你需要使用 LocationListener
來請求更新.鑒于您只需要一個位置,您可以在第一個值返回后取消注冊 LocationListener.
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.
不過,在實踐中,多聽一會兒可能是明智之舉.基于位置的服務(尤其是 GPS)在他們第一次得到修復時傾向于跳來跳去".最好的辦法是監聽一定的時間,或一定數量的更新,或者直到達到一定的準確度(位置監聽器將返回所返回位置的準確度).
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).
這篇關于Android:getLastKnownLocation 已過期 - 如何強制刷新位置?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!