Mapbox iOS:打开设备定位服务后用户位置未显示

Mapbox iOS: User location not showing after turning device location service on

当设备定位服务关闭时,用户打开了我使用 iOS Mapbox 和 Mapbox Navigation SDK 的应用程序。用户无法在地图上看到其设备位置,因为定位服务已关闭。当用户转到设备设置并打开位置服务然后返回应用程序时,用户位置仍然不可见。当我通过 mapView.userLocation.coordinate 检查用户位置坐标时,我得到一个无效坐标。即使我调用 locationManager.startUpdatingLocation():

也不会调用下面的委托方法
func mapView(_ mapView: MGLMapView, didUpdate userLocation: MGLUserLocation?)

我可以通过删除旧地图视图并设置新地图视图来解决问题。这意味着当用户在转到“设置”并打开定位服务后返回应用程序时,我必须删除旧的地图视图并设置另一个以显示用户位置。但是我不想那样做。

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
    if status == .authorizedWhenInUse || status == .authorizedAlways {
        isLocationServicesEnabled = true
        if mapView != nil {
           mapView.removeFromSuperview()
           mapView = nil
        }
        resetupMapView()
    } 
}

打开定位服务后,如何在地图视图中显示用户位置?有什么方法可以重新加载地图视图吗?

当我尝试使用下面的代码在地图上显示用户位置时,出现崩溃并显示以下消息:

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
    if status == .authorizedWhenInUse || status == .authorizedAlways {
        if mapView != nil {
            mapView.showsUserLocation = true
        }
    } 
}

由于未捕获的异常而终止应用程序 'MGLUserLocationAnnotationTypeException',原因:'用户位置注释视图必须是一种 MGLUserLocationAnnotationView。我不知道为什么会这样。我还实现了 viewForAnnotation 委托方法来显示用于注释的自定义图像。

func mapView(_ mapView: MGLMapView, viewFor annotation: MGLAnnotation) -> MGLAnnotationView? {
    let reuseIdentifier = "reusableView"
    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseIdentifier)

    if annotationView == nil {
        annotationView = MGLAnnotationView(reuseIdentifier: reuseIdentifier)
        let markerImageView = UIImageView(image: UIImage(named: "Orange"))
        markerImageView.frame = CGRect(x: 0, y: 0, width: viewModel.markerImageWidth, height: viewModel.markerImageHeight)
        annotationView?.frame = markerImageView.frame
        annotationView?.center = markerImageView.center
        annotationView?.addSubview(markerImageView)
    }
    return annotationView
}

mapView 有一个名为 showsUserLocation 的 Bool 属性 即使在打开位置服务后,如果此 属性 设置为 false 那么用户的位置将不会显示。

mapView.showsUserLocation = true 添加到您的 didChangeAuthorization 方法中。

****** 编辑 ******

viewFor annotation 在地图添加每个注释时调用,甚至是 userAnnotation。但是 userAnnotation 需要是 MGLUserLocationAnnotationView。如果您在注释中使用自定义图像但不想向 userAnnotation 添加图像,则需要在 viewFor annotation 方法中将其排除。最简单的方法是:

    func mapView(_ mapView: MGLMapView, viewFor annotation: MGLAnnotation) -> MGLAnnotationView? {
        // We're not concerned with the user annotation.
        guard annotation is YOUR-ANNOTATION-TYPE else { return nil }

        let reuseIdentifier = "reusableView"
        var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseIdentifier)

       if annotationView == nil {
           annotationView = MGLAnnotationView(reuseIdentifier: reuseIdentifier)
           let markerImageView = UIImageView(image: UIImage(named: "Orange"))
           markerImageView.frame = CGRect(x: 0, y: 0, width: viewModel.markerImageWidth, height: viewModel.markerImageHeight)
           annotationView?.frame = markerImageView.frame
           annotationView?.center = markerImageView.center
           annotationView?.addSubview(markerImageView)
       }
       return annotationView
    }

如果您确实想为您的 userAnnotation 分配自定义图像,请参阅 Mapbox 网站上的这个示例,其中说明了如何操作。 Custom User Annotation