Android:getLastKnownLocation 已过期 - 如何强制刷新位置?

Android: getLastKnownLocation out-of-date - how to force location refresh?(Android:getLastKnownLocation 已过期 - 如何强制刷新位置?)
本文介绍了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 已过期 - 如何强制刷新位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How can I use CClistview in COCOS2d Android?(如何在 COCOS2d Android 中使用 CClistview?)
cocos2d-android: how to display score(cocos2d-android:如何显示分数)
Sqlite database not copied from asset folder Android(Sqlite 数据库未从资产文件夹 Android 复制)
SQLite Database Copy Appears Corrupted When Generated by Device and not Emulator(SQLite 数据库副本在由设备而不是模拟器生成时出现损坏)
Android file copy(安卓文件拷贝)
Android how to detect Copy event of Edittext in android(Android如何在android中检测Edittext的Copy事件)