首页 文章

iPhone GPS用户位置在手机静止时来回移动

提问于
浏览
1

我正在做一些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

你知道是否有设置或我可以改变以防止这种来回行为 . 当手机静止时,即使用户引脚每隔几秒左右也会左右移动 .

或者还有什么我需要编码才能解释这种疯狂的大摇大摆?

1 回答

  • 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
    }
    

相关问题