安卓:getLastKnownLocation外的日期 - 如何强制位置刷新? [英] Android: getLastKnownLocation out-of-date - how to force location refresh?

查看:349
本文介绍了安卓: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).

这篇关于安卓:getLastKnownLocation外的日期 - 如何强制位置刷新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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