問(wèn)題描述
我正在使用
myLocation = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
在我的應(yīng)用程序啟動(dòng)時(shí)檢索當(dāng)前位置.
to retrieve the current location at the start-up of my application.
正如 Android 文檔所述,此位置可能過(guò)時(shí)",因?yàn)樵摲椒ǚ祷厣洗问褂?GPS 時(shí)的位置.
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.
我如何主動(dòng)從 GPS 請(qǐng)求當(dāng)前位置?我考慮過(guò)使用 LocationListener
,但這可能有點(diǎn)矯枉過(guò)正,因?yàn)槲抑恍枰淮挝恢?在我的應(yīng)用程序開(kāi)始時(shí)).
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).
有什么想法嗎?
推薦答案
你最初的直覺(jué)是正確的——你需要使用 LocationListener
來(lái)請(qǐng)求更新.鑒于您只需要一個(gè)位置,您可以在第一個(gè)值返回后取消注冊(cè) 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.
不過(guò),在實(shí)踐中,多聽(tīng)一會(huì)兒可能是明智之舉.基于位置的服務(wù)(尤其是 GPS)在他們第一次得到修復(fù)時(shí)傾向于跳來(lái)跳去".最好的辦法是監(jiān)聽(tīng)一定的時(shí)間,或一定數(shù)量的更新,或者直到達(dá)到一定的準(zhǔn)確度(位置監(jiān)聽(tīng)器將返回所返回位置的準(zhǔn)確度).
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).
這篇關(guān)于Android:getLastKnownLocation 已過(guò)期 - 如何強(qiáng)制刷新位置?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!