問(wèn)題描述
啟用 GPS 后,需要一些時(shí)間才能準(zhǔn)備好 GPS.所以使用這個(gè)代碼可能會(huì)得到一個(gè)過(guò)時(shí)的位置:位置 = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);我無(wú)法使用已過(guò)期的位置.如何獲得我需要的位置?
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?
這是一個(gè)類(lèi)似的問(wèn)題:Android:getLastKnownLocation 過(guò)期 - 如何強(qiáng)制位置刷新?
從中,我可以使用 LocationListener 獲取更新的位置,該位置應(yīng)該不會(huì)過(guò)時(shí).這是正確的.但是我該如何處理這種情況:
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:
我已經(jīng)啟用了 GPS,然后啟動(dòng)我的應(yīng)用程序.如果我呆在一個(gè)地方不動(dòng),那么 onLocationChanged() 將不會(huì)被調(diào)用.所以不能用這個(gè)方法來(lái)判斷getLastKnownLocation()的返回值是不是過(guò)期了?
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?
有什么想法嗎?
推薦答案
如何獲取未過(guò)期的位置?
How to get location that isn't out of date?
您調(diào)用 requestLocationUpdates()
的 minTime 為 1000 毫秒,對(duì)于 onLocationChanged()
中的每個(gè) Location
,您調(diào)用 getAccuracy()代碼>.如果您可以接受準(zhǔn)確度,請(qǐng)調(diào)用
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()
.
所以我不能用這個(gè)方法來(lái)判斷getLastKnownLocation()的返回值是否過(guò)時(shí)
So I can't use this method to judge whether the return value of getLastKnownLocation() is not out-of-date
我認(rèn)為它返回的 Location 對(duì)象將包含一個(gè) 時(shí)間戳.您可以為現(xiàn)在與位置的 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()
這篇關(guān)于如何獲取未過(guò)期的位置?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!