LocationManager 返回旧的缓存“Wifi”具有当前时间戳的位置

发布于 2024-11-23 22:21:26 字数 730 浏览 1 评论 0原文

我正在尝试获取当前位置。为此,我实现了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夕嗳→ 2024-11-30 22:21:27

这是我遇到过的一个已知问题,并对为什么会发生这种情况进行了一些研究。

以下是我的观察:

  • 通常,当失去网络连接后发生移动网络切换时,就会发生这种情况,这可能不一定足以让用户意识到。
  • 假设你正在乘坐地铁,你在 A 站上车,在 B 站下车,现在当你在 B 站下车时,网络小区 ID 可能/可能不再是 A 站,当然它会做一个手动操作: 但是,如果您在交接
  • 之前调用 getLocation 处于活动状态,您将获得 A 站位置,该位置可能在 10 公里 15 分钟后。

首先了解网络定位的工作原理:
Android 具有当前连接到的塔的 cellId,然后 Google 使用该 ID 来执行查找并获取大致位置信息,其精度范围从 50 米(最好的之一)到几千米。如果 cellId 不正确(如上例所示),那么您将收到错误的位置。

除了使用可以消除这种噪音的自定义算法之外,您没有什么办法可以避免这种情况。像这样的东西

if (location from network) {
    if (speed obtained from the difference between previous and current location is        greater than say 30 m/s) {
        ignore this location as noise
    } else {
       location is correct
    }
}

This is a known issue which I have encountered and did some research on why this happens.

Here are my observations:

  • Usually this happens when the mobile network hand-off is happening after losing network connectivity which may not necessarily be significant enough for the user to realize.
  • Consider you are taking a tube train and you get in at station A and get down at station B, now when you get down at station B the network cell ID may/maynot still be of station A and of course it will do a hands-off and move to station B.
  • However if you call for getLocation is active before the hand-off you would get station A location which might be like 10 km and 15 mins back.

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

if (location from network) {
    if (speed obtained from the difference between previous and current location is        greater than say 30 m/s) {
        ignore this location as noise
    } else {
       location is correct
    }
}
探春 2024-11-30 22:21:27

这很有帮助:

深入了解位置

最后是该演讲的源代码:

android-protips-location

This is helpful:

A Deep Dive Into Location

and lastly the source code for that talk:

android-protips-location

别念他 2024-11-30 22:21:27

在我对代码进行一些更改之前,我一直面临同样的问题。

发生的情况是,当我请求 GPS 和网络位置更新时,我附加了相同的 LocationListener,并且遇到了“奇怪”的问题,包括获取当前时间的旧 WIFI 位置更新。

这是我的旧代码:

locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 0, locationListener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 0, locationListener);

显然,这是一件相当“不安全”的事情(对不起,这里是 Android 新手),所以我将其更改为:

locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 0, networkLocationListener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 0, gpsLocationListener);

当然,我必须定义 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:

locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 0, locationListener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 0, locationListener);

Apparently that is a rather "unsafe" thing to do (sorry, Android newbie here) and so I changed it to:

locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 0, networkLocationListener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 0, gpsLocationListener);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文