在 iOS 中获取准确位置

Get accurate location in iOS

我使用此设置设置位置精度:

locationManager.desiredAccuracy = kCLLocationAccuracyBest

然后我得到了这个位置:

if let location = locations.first {
....
}

但是它多次更新位置,其中很多都不准确。例如,当我找到位置时,它距离设备位置大约 100 米。

如何获得准确的位置?

我知道前几个位置不准确,直到设备修复了位置。

查看位置 (CLLocation) 的 horizo​​ntalAccuracy 属性。这是以米为单位测量的。您可以使用此 属性 忽略不够准确的位置读数。

根据多年来在核心位置的经验。 GPS 芯片 "Warm up" 需要一些时间,有时会在锁定之前报告多个位置。您将更多地在室内体验这种体验,而不是在清晰的天空视野中。带有金属屋顶的建筑物也会影响精度。但是,如果使用 distanceFilter 属性(以米为单位指定)distanceFilter and setting desired accuracy using this property also horizontalAccuracy.

,您可以忽略 gps 更新,如果它们是 in/out 某个阈值

来自the documentation

The receiver does its best to achieve the requested accuracy; however, the actual accuracy is not guaranteed.

因此,您无法保证检测到的位置有多接近,但您可以尝试其他一些方法:

  1. 使用kCLLocationAccuracyBestForNavigation。这应该能让您获得更准确的读数,但会消耗更多电池。

  2. 过滤位置,虽然我不知道这是否可行,因为您拥有的唯一数据是从您正在使用的位置管理器收到的数据。

您可以检查每次更新的准确性,并过滤位置,直到它们达到可靠的准确性:

    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    print(manager.location!.horizontalAccuracy)

    if locations.last!.horizontalAccuracy <= manager.desiredAccuracy{
        print(manager.location)         
    }
}