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