LocationManager 返回旧的缓存“Wifi”具有当前时间戳的位置
我正在尝试获取当前位置。为此,我实现了一个 LocationListener 并将其注册到网络和 GPS 提供商:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
然后我阻塞 30 秒,并使用传递到侦听器
onLocationChanged()
方法的第一个位置,精度为 100 米或更好。
大多数时候这工作得很好。如果手机连接到某个 Wifi 网络,只需一秒钟即可获得正确位置,精度约为 50 米。如果没有 Wifi 但启用了 GPS,当然可能需要一段时间才能获取位置。
然而,有时,当连接到 Wifi 并获取当前位置时,会提供一些旧的(缓存的?)以前的“Wifi”位置 - 它可能是 15 分钟前的位置,距离当前位置 15 公里。 问题是,它
location.getTime()
返回当前时间 - 因此不可能知道该位置是旧的。
我想我必须实施一个更复杂的解决方案 - 我只是想知道为什么这些旧的“Wifi”位置具有当前时间戳,而不是最初检索时的时间戳。
I am trying to get the current location. For that I implement a LocationListener and register it for both the network and the GPS provider:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
I block then for 30 seconds and use the first location that gets passed into the listener's
onLocationChanged()
method with an accuracy of 100 meters or better.
Most of the time this works fine. If the phone is connected to some Wifi network, it takes just a second to get a correct location with an accuracy of about 50 meters. If there is no Wifi but GPS is enabled, it can of course take a while to get a location.
Sometimes however, when connected to a Wifi and getting the current location, some old (cached?) previous "Wifi" location is provided - it might be 15 minutes old and 15 kilometers away from the current location.
The problem is, that
location.getTime()
returns the current time - so it is impossible to know that the location is old.
I guess I have to implement a more elaborate solution - I would just like to know why these old "Wifi" locations have a current timestamp instead one from the time when it was originally retrieved.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是我遇到过的一个已知问题,并对为什么会发生这种情况进行了一些研究。
以下是我的观察:
首先了解网络定位的工作原理:
Android 具有当前连接到的塔的 cellId,然后 Google 使用该 ID 来执行查找并获取大致位置信息,其精度范围从 50 米(最好的之一)到几千米。如果 cellId 不正确(如上例所示),那么您将收到错误的位置。
除了使用可以消除这种噪音的自定义算法之外,您没有什么办法可以避免这种情况。像这样的东西
This is a known issue which I have encountered and did some research on why this happens.
Here are my observations:
First understand how network location works:
Android has the cellId of the tower to which it is currently connected to and this id is then used by google to perform look-up and fetch approximate location information whose accuracy can range from 50 metres (one of the best) to a few thousand metres. If the cellId is incorrect as shown in the above example then you would receive wrong location.
There is not much you can do to avoid this except having a custom algorithm that can weed out this noise. Something like
这很有帮助:
深入了解位置
最后是该演讲的源代码:
android-protips-location
This is helpful:
A Deep Dive Into Location
and lastly the source code for that talk:
android-protips-location
在我对代码进行一些更改之前,我一直面临同样的问题。
发生的情况是,当我请求 GPS 和网络位置更新时,我附加了相同的 LocationListener,并且遇到了“奇怪”的问题,包括获取当前时间的旧 WIFI 位置更新。
这是我的旧代码:
显然,这是一件相当“不安全”的事情(对不起,这里是 Android 新手),所以我将其更改为:
当然,我必须定义 2 个单独的 onLocationChanged 代码块来处理 2 个侦听器。
嗯,它确实解决了我的问题。我在 Gingerbread 上对此进行了测试(API 级别:8)。不确定它是否适合你。
I have been facing the same issues until I made some changes to my code.
What happened is that I was attaching the same LocationListener when requesting for both GPS and Network location updates and I was getting "weird" issues including getting old WIFI location updates with current time.
Here's my old code:
Apparently that is a rather "unsafe" thing to do (sorry, Android newbie here) and so I changed it to:
Of course I had to define 2 separate onLocationChanged block of codes to handle the 2 listeners.
Well, it did solve my problem. I tested this on Gingerbread (API Level: 8). Not sure if it works for you.