MapView gps中初始用户位置错误

问题描述 投票:0回答:1

我想获取用户位置,这就是我所做的:我在ViewController中设置locationManager:

var locationManager = CLLocationManager()

func setUpLocationManager() {
    locationManager.delegate = self
    locationManager.distanceFilter = 5
    locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
    mapView?.showsUserLocation = true
    locationManager.startUpdatingLocation()
}

这里是更新位置时调用的函数:

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    if !locations.isEmpty {
            print("\(locations[0])")
        }
    }
}

它打印出用户位置的初始位置(见图),但是我认为这是错误的,因为它是一个近似值。我也尝试过:

func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) {
    let accuracy = userLocation.location?.horizontalAccuracy
    if accuracy == locationManager.desiredAccuracy {
        print("\(locations[0])")
    }
}

但是它从不打印位置,因为locationManager.desiredAccuracy等于-2并且accuracy总是> 0。

这是我在mapView上看到的:

enter image description hereenter image description here

最初,用户图钉显示的位置接近实际位置(但不正确),随后用户图钉移动到实际用户位置。我希望在用户引脚位于正确位置时调用函数didUpdateLocations。我的问题类似于this,但在Objective-C中。

有任何提示吗?谢谢

gps mkmapview cllocationmanager mkuserlocation
1个回答
0
投票

参考MKMapView's user location is wrong on startup or resume

快速版本:

func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) {
    let accuracy = userLocation.location?.horizontalAccuracy
    if accuracy ... {

    }
}
© www.soinside.com 2019 - 2024. All rights reserved.