【问题标题】:Wrong initial user location in MapView gpsMapView gps 中的初始用户位置错误
【发布时间】:2020-04-03 01:48:45
【问题描述】:

我想获取用户位置,这就是我所做的: 我在 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 上看到的:

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

有什么提示吗?谢谢

【问题讨论】:

    标签: swift gps mkmapview cllocationmanager mkuserlocation


    【解决方案1】:

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

    Swift 版本:

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

    【讨论】:

    • 谢谢,但我不明白在 if 条件中输入什么,因为如果我将它与 locationManager.desiredAccuracy 进行比较,它总是返回 false,因为 desiredAccuracy 等于 -2。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-08
    • 1970-01-01
    • 2011-11-07
    • 2016-07-01
    • 2016-11-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多