首页 > 解决方案 > CLLocationManager didUpdateLocations 委托中没有收到稳定的位置更新。我不知道出了什么问题

问题描述

我正在尝试使用 locationManager(_ manager:CLLocationManager,didUpdateLocations 位置:[CLLocation])获取位置更新。收到位置。但是当我在设备中调试应用程序时,我记录了一些奇怪的事件。

一是在调试过程中,手机从未从原位移开,但它在 15 分钟内记录了不同的位置,总距离约为 50 米。

另一个是代表第一次被解雇时,它提供了正确的当前位置,但很快它移动了大约 800 米,然后在一段时间后恢复到正确的位置。

我在这个应用程序中要做的是计算随时间推移的行进距离。但是由于我没有得到正确的数据,所以我得到的距离是错误的。

位置管理器一直是这样使用的

    locationManager = CLLocationManager()
    locationManager.allowsBackgroundLocationUpdates = true
    locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
    locationManager.distanceFilter = 1
    locationManager.activityType = .automotiveNavigation
    locationManager.pausesLocationUpdatesAutomatically = true

并保存和使用委托的 lastLocation

 public func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    if let bestLocation = locations.last {
        guard -(bestLocation.timestamp.timeIntervalSinceNow) < 5 || bestLocation.horizontalAccuracy > 0 else {
            return
        }
        self.lastLocation = bestLocation
    }
}

我正在保存当前位置数据并将其与每隔一秒收到的下一个位置数据进行比较以计算距离。

那么如何修复这个不正确的接收位置并正确计算距离。我这样做的方法可能是错误的,所以请建议我更好的处理方法。

标签: swiftcore-locationcllocationmanager

解决方案


推荐阅读