How to get location that isn#39;t out of date?(如何获取未过期的位置?)
问题描述
启用 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()
这篇关于如何获取未过期的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何获取未过期的位置?


基础教程推荐
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01