计算行进的总距离不准确

时间:2016-03-18 21:06:17

标签: ios swift

使用以下代码,行驶距离始终低于行驶至少几英里时行驶的实际距离20-40%。这是在一个大城市旅行,整个行程都有强大的小区和GPS信号,所以我知道这不是问题。我使用Google地图和MapMyHike验证了实际距离。有任何想法吗? 下面粘贴代码的相关部分:

let metersToMiles: Double = 0.000621371
var startLocation: CLLocation!
var lastLocation: CLLocation!
var distanceTraveled: Double = 0


override func viewDidLoad() {
    super.viewDidLoad()

    self.locationManager.requestAlwaysAuthorization() // Location permission for background
    self.locationManager.requestWhenInUseAuthorization() // Location permission for foreground

    if CLLocationManager.locationServicesEnabled() {
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
        locationManager.distanceFilter = kCLDistanceFilterNone
        locationManager.activityType = CLActivityType.Fitness
        locationManager.pausesLocationUpdatesAutomatically = false
    }
}

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    if startLocation == nil {
        print("startLocation is null")
        startLocation = locations.first
    } else {
        let lastLocation = locations.last
        let distance = startLocation.distanceFromLocation(lastLocation!)
        startLocation = lastLocation
        distanceTraveled += distance
    }
    updateDistanceTraveledText()
}

func updateDistanceTraveledText() {
    let distanceTraveledString = "\(String(format:"%.1f", distanceTraveled * metersToMiles))"
    distanceTraveledText.text = distanceTraveledString
}

我还尝试了所有类型的所需类型,如kCLLocationAccuracyBest和kCLLocationAccuracyNearestTenMeters等,并且根本没有设置activityType,并且还将它设置为AutomotiveNavigation / Fitness都无济于事。

1 个答案:

答案 0 :(得分:1)

您可能会获得一系列位置更新,因为它会在您的精确位置上显示为零。这些更新表明精度的变化,而不是实际的运动 - 但看起来你的代码将把它们注册为运动。无论您设置desiredAccuracy多高,都会发生这种情况,因为it’s a maximum accuracy, not a minimum

您可以通过记录horizontalAccuracy的{​​{1}}和verticalAccuracy属性进行调试。

如果问题确实是您获得的报告具有不同的准确性,一些可能的解决方案是:

  • 使用CLLocations上的distace过滤器忽略小行程。
  • 根据您设置的某个阈值,丢弃任何不够准确的CLLocationManager
  • 如果最新位置在前一个位置的准确度范围内(因此可能不代表实际动作),替换最后一个位置而不是添加新位置。
  • 使用某种复杂的贝叶斯推理来找到所有报告点的最大可能性路径。

如果这是偶然的话,我会做#1或#2;如果你真的在意,我会做#3;如果最高准确度是必要的话,我会做#4 ...而且你对数学的了解比我更多。

相关问题