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

Mapbox iOS:打开设备位置服务后未显示用户位置,ios,swift,mapbox,userlocation,Ios,Swift,Mapbox,Userlocation,当设备位置服务关闭时,用户打开使用iOS Mapbox和Mapbox导航SDK的我的应用程序。用户无法在地图上看到其设备位置,因为位置服务已关闭。当用户转到设备设置并打开位置服务,然后返回应用程序时,用户位置仍然不可见。通过mapView.userLocation.coordinate检查用户位置坐标时,得到的坐标无效。即使在调用locationManager.startUpdatingLocation()时,也不能调用下面的委托方法: 我可以通过删除旧的地图视图并设置新的地图视图来解决此问题。

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

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

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
}

地图视图有一个名为
showsUserLocation
的Bool属性,即使在打开位置服务之后,如果此属性设置为
false
,则不会显示用户的位置

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

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

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

    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网站上的示例,该示例解释了如何执行此操作

谢谢你的回复,我以前试过,但它会导致应用程序崩溃,因为用户位置是无效坐标。你是在用真人手机还是模拟器?我在用真人手机,iPhone 8 IOS12你能告诉我你遇到了什么线路的崩溃吗?如果位置服务和showsUserLocation都正常,则您可能试图过早访问用户位置。您是否实现了
mapView:didUpdateUserLocation
?我编辑了这个问题。是的,我实现了mapView:didUpdateUserLocation,但它不能被调用。
    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
    }