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()
这篇关于如何获取未过期的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何获取未过期的位置?
基础教程推荐
- Android:STATE_SELECTED不工作 2022-01-01
- - (CGRect)convertRect:(CGRect)rect toView:(UIView *)view 如何工作 2022-01-01
- 可可/目标C(OSX不是iOS)从子对象访问父对象 2022-01-01
- 在 appComponent dagger 2 中动态添加测试模块? 2022-01-01
- 新的@SystemApi 注解是什么意思,和@hide 有什么区别 2022-01-01
- 在 Android 模拟器中激活网络位置提供程序? 2022-01-01
- 在 iOS8 中无法获得正确的键盘高度值 2022-01-01
- 突出显示朗读文本(在 iPhone 的故事书类型应用程序中) 2022-01-01
- 如何将多个组件添加到 PickerView? 2022-01-01
- 我的 UIImageView 的任务 2022-01-01
