微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 CLLocationManager didUpdateLocations 委托中没有收到稳定的位置更新我不知道出了什么问题

如何解决在 CLLocationManager didUpdateLocations 委托中没有收到稳定的位置更新我不知道出了什么问题

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

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

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

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

locationmanager 已经这样使用了

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

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。