Android:getLastKnownLocation 已过期 - 如何强制刷新位置? [英] Android: getLastKnownLocation out-of-date - how to force location refresh?

查看:28
本文介绍了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 已过期 - 如何强制刷新位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆