Swift iOS,CLLocationManager didUpdateLocations记录未访问的位置

时间:2017-01-19 12:59:45

标签: ios swift cllocationmanager

在我用于XCode8 for iOS的Swift 3中编写的GPS应用程序中,CLLocationManager didUpdateLocations被称为正常,但我发现它经常接受我没有访问的位置(在iOS 9中测试)

下面是我使用相关记录位置设置的MKMapView的图片

http://imgur.com/eLYUAqy

  • 红点=开始(向北行驶)

  • 蓝点=完成(沿着我向北行进的路径返回)

  • Green Dots =从未走过这么远的南方

注意:我在录制时开车,而不是跑步。不确定这是否重要

所以在我回家的路上,我从红点附近转到我家的蓝点,我从不在我家的南边。 我想知道为什么CLLocationManager记录器位于绿点时我没有走这么远的南方(我已经多次记录这条路径而且这种情况一直在发生,也许是假设我到达70米外的十字路口?)

我正在绘制我的距离覆盖范围与持续时间和速度与所覆盖的距离之间的关系,因此我也看到了这些结尾处的尖峰,因为在我转入我家的短时间内,CLLocationManager假设我已经移动到了交叉点底部的绿色圆点再回来,包括所有这些距离。

我尝试过更改distanceFilter和horizo​​ntalAccuracy,但这并没有解决问题。从我在网上看到的,我猜我可能需要以某种方式平滑我的数据或过滤掉不切实际的加速,但我不确定如何实现这一点。

以下是我的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)}}}

1 个答案:

答案 0 :(得分:0)

直言不讳地说,iOS GPS就是这样。它有时会给出虚假的读数。你必须教你的应用程序来过滤掉不好的读数。

您应该检查每个位置更新的“水平精度”读数。这是一个“混乱的半径”值,以米为单位。 真实位置可以是具有该半径的圆内的任何位置。因此,如果值为500,则意味着您的真实位置可能位于半径为500米的圆内,也可能位于环绕一公里的圆圈中的任何位置。

当卫星信号变弱时,GPS读数的质量下降(奇怪的是,较大的“水平精度”值意味着GPS读数较差。)您需要确定您对不良GPS信号的容忍度,并拒绝读数比你的宽容更穷。此时,您应该向用户显示“弱GPS信号”消息。