【发布时间】:2017-06-04 04:30:48
【问题描述】:
在我用 Swift 3 在 XCode8 for iOS 上编写的 GPS 应用程序中,CLLocationManager didUpdateLocations 被调用得很好,但我发现它经常接受我没有访问过的位置(在 iOS 9 中测试)
下面是我设置的带有相关记录位置的 MKMapView 的图片
红点 = 起点(向北行驶)
蓝点 = 完成(沿着我向北行驶的相同路径返回后)
绿点 = 从来没有去过这么远的南方
注意:我在录制这段视频时正在开车,而不是在跑步。不确定这是否重要
所以在回家的路上,我从红点附近转向蓝点所在的房子,我从不向南走。 我想知道当我没有向南走这么远的时候,为什么 CLLocationManager 记录器位置在绿点(我已经多次记录了这条路径并且这种情况一直在发生,也许是假设我正在到达 70m 外的十字路口?)
我正在绘制我的行驶距离与持续时间和速度与行驶距离的关系图,所以我也看到了这些结束时的尖峰,因为在很短的时间内我转身进入我的房子,CLLocationManager 假设我已经搬到了十字路口底部的绿点,然后又回来,包括了所有的距离。
我已尝试更改 distanceFilter 和 HorizontalAccuracy 但这并没有解决问题。根据我在网上阅读的内容,我猜我可能需要以某种方式平滑我的数据或过滤掉不切实际的加速度,但我不确定我将如何实现这一点。
下面是我的 CLLocationManager 的相关代码。任何有关如何解决此问题的帮助都会很棒,谢谢!
var distance = 0.0
@IBOutlet weak var mapView: MKMapView!
lazy var locationManager: CLLocationManager = {
var _locationManager = CLLocationManager()
_locationManager.delegate = self
_locationManager.desiredAccuracy = kCLLocationAccuracyBest
_locationManager.activityType = .fitness
_locationManager.distanceFilter = 10.0
return _locationManager}()
lazy var locations = [CLLocation]()
extension GPSTracker: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
for location in locations {
let howRecent = location.timestamp.timeIntervalSinceNow
let accuracy = location.horizontalAccuracy
if abs(howRecent) < 10 && accuracy < 10 && accuracy > 0 && location.verticalAccuracy > 0 {
let region = MKCoordinateRegionMakeWithDistance(location.coordinate, 500, 500)
mapView.setRegion(region, animated: true)
if self.locations.count > 0 {
distance += location.distance(from: self.locations.last!)
var coords = [CLLocationCoordinate2D]()
coords.append(self.locations.last!.coordinate)
coords.append(location.coordinate)
mapView.add(MKPolyline(coordinates: &coords, count: coords.count))}
self.locations.append(location)}}}
【问题讨论】:
标签: ios swift cllocationmanager