【问题标题】:iPhone GPS User Location is moving back and forth while the phone is stilliPhone GPS 用户位置在手机静止时来回移动
【发布时间】:2016-01-02 15:16:00
【问题描述】:

我正在做一些 mapkit 和 corelocation 编程来绘制用户路线。例如。他们去散步,它显示了他们所走的路。

在模拟器上一切正常 100%。

在 iPhone 上我遇到了一个大问题,我不知道该怎么办。为了确定用户是否已经“停止”,我基本上检查了一段时间内速度是否(几乎)为 0。

但是,仅保留手机仍然会为新更新的位置更改吐出此日志(来自位置管理器代表)。这些是 locationManager(_:didUpdateLocations:) 回调中的连续更新。

speed 0.021408926025254 with distance 0.192791659974976
speed 0.0532131983839802 with distance 0.497739230237728
speed 11.9876451887096 with distance 15.4555990691609
speed 0.230133198005176 with distance 3.45235789063791
speed 0.0 with distance 0.0
speed 0.984378335092039 with distance 11.245049843458
speed 0.180509147029171 with distance 2.0615615724029
speed 0.429749086272364 with distance 4.91092459284206

现在我将准确度设置为最佳:

_locationManager                    = CLLocationManager()
_locationManager.delegate           = self
_locationManager.distanceFilter     = kCLDistanceFilterNone
_locationManager.desiredAccuracy    = kCLLocationAccuracyBest

您知道是否有设置或者我可以更改以防止这种来回行为。甚至当手机静止时,用户 pin 也会每隔几秒左右疯狂移动。

或者我还需要编写其他代码来解释这种狂野的招摇吗?

【问题讨论】:

  • 这就是 GPS 的工作原理。它不像模拟器中的假数据那样 100% 准确。 GPS 可以返回 10 米以外的值,即使在静止不动时也是如此。
  • 那么我如何判断某人是否“静止不动”?
  • 你真的不能。例如,无法判断某人是静止不动还是在 5 米半径范围内移动。
  • 啊,这是个好主意。我可以创建一个半径,如果它们在该半径内停留 X 时间。谢谢rmaddy!

标签: ios swift gps mapkit core-location


【解决方案1】:

我检查用户是否在一定时间内移动了一定距离以确定他们是否已经停止(感谢 rmaddy 提供的信息):

/**
    Return true if user is stopped. Because GPS is in accurate user must pass a threshold distance to be considered stopped.
*/
private func userHasStopped() -> Bool
{
    // No stop checks yet so false and set new location
    if (_lastLocationForStopAnalysis == nil)
    {
        _lastLocationForStopAnalysis = _currentLocation
        return false
    }

    // If the distance is greater than the 'not stopped' threshold, set a new location
    if (_lastLocationForStopAnalysis.distanceFromLocation(_currentLocation) > 50)
    {
        _lastLocationForStopAnalysis = _currentLocation
        return false
    }

    // The user has been 'still' for long enough they are considered stopped
    if (_currentLocation.timestamp.timeIntervalSinceDate(_lastLocationForStopAnalysis.timestamp) > 180)
    {
        return true
    }

    // There hasn't been a timeout or a threshold pass to they haven't stopped yet
    return false
}

【讨论】:

  • 还要注意可怕的水平精度读数。 (它应该被称为水平误差范围或类似的东西。当误差范围很大时,位置会漂移得更多。我建议忽略
猜你喜欢
  • 2014-06-24
  • 2017-04-16
  • 2016-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-21
  • 2021-04-01
  • 1970-01-01
相关资源
最近更新 更多