Android: getLastKnownLocation out-of-date - how to force location refresh?(Android:getLastKnownLocation 已过期 - 如何强制刷新位置?)
问题描述
我正在使用
myLocation = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
在我的应用程序启动时检索当前位置.
to retrieve the current location at the start-up of my application.
正如 Android 文档所述,此位置可能过时",因为该方法返回上次使用 GPS 时的位置.
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.
我如何主动从 GPS 请求当前位置?我考虑过使用 LocationListener,但这可能有点矫枉过正,因为我只需要一次位置(在我的应用程序开始时).
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).
有什么想法吗?
推荐答案
你最初的直觉是正确的——你需要使用 LocationListener 来请求更新.鉴于您只需要一个位置,您可以在第一个值返回后取消注册 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.
不过,在实践中,多听一会儿可能是明智之举.基于位置的服务(尤其是 GPS)在他们第一次得到修复时倾向于跳来跳去".最好的办法是监听一定的时间,或一定数量的更新,或者直到达到一定的准确度(位置监听器将返回所返回位置的准确度).
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).
这篇关于Android:getLastKnownLocation 已过期 - 如何强制刷新位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Android:getLastKnownLocation 已过期 - 如何强制刷新位置
基础教程推荐
- 如何将多个组件添加到 PickerView? 2022-01-01
- 我的 UIImageView 的任务 2022-01-01
- 新的@SystemApi 注解是什么意思,和@hide 有什么区别 2022-01-01
- 可可/目标C(OSX不是iOS)从子对象访问父对象 2022-01-01
- Android:STATE_SELECTED不工作 2022-01-01
- 在 iOS8 中无法获得正确的键盘高度值 2022-01-01
- - (CGRect)convertRect:(CGRect)rect toView:(UIView *)view 如何工作 2022-01-01
- 突出显示朗读文本(在 iPhone 的故事书类型应用程序中) 2022-01-01
- 在 Android 模拟器中激活网络位置提供程序? 2022-01-01
- 在 appComponent dagger 2 中动态添加测试模块? 2022-01-01
