swift - 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
}
}
我正在保存当前位置数据并将其与每隔一秒收到的下一个位置数据进行比较以计算距离。
那么如何修复这个不正确的接收位置并正确计算距离。我这样做的方法可能是错误的,所以请建议我更好的处理方法。
解决方案
推荐阅读
- javascript - 按特定属性值嵌套的对象的递归过滤数组
- elasticsearch - 如何修复错误“[标准]令牌过滤器已被删除”?
- css - 使用 useState 在 React 中将背景图像添加到导航链接
- java - Drive API Watch 方法 Java 中的错误 403 dailyLimitExceededUnreg
- json - json 验证器抱怨此解析错误
- observable - Ngrx:如何订阅组件中的一些操作?
- perl - 使用相同散列的元素声明散列
- discord.py - on_ready 上的 discord.py Music Bot 加入指定频道并使用 youtube-dl 流式传输 mp3
- extjs - 在调整大小之前清空网格单元格
- html - 按优先级排序重复的选定字母值