【问题标题】:Mapbox iOS: User location not showing after turning device location service onMapbox iOS:打开设备定位服务后未显示用户位置
【发布时间】:2018-12-29 07:29:56
【问题描述】:

当设备定位服务关闭时,用户打开我使用 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
}

【问题讨论】:

    标签: ios swift mapbox userlocation


    【解决方案1】:

    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

    【讨论】:

    • 感谢您的回复,我之前尝试过,但由于用户位置是无效坐标,导致应用崩溃。
    • 你用的是真机还是模拟器?
    • 我用的是真机,iPhone 8 iOS12
    • 你能说明你遇到崩溃的那一行吗?如果位置服务和 showUserLocation 都正常,您可能试图过早访问用户位置。你实现mapView:didUpdateUserLocation了吗?
    • 我编辑了这个问题。是的,我实现了 mapView:didUpdateUserLocation 但它没有被调用。
    猜你喜欢
    • 1970-01-01
    • 2015-10-04
    • 2016-10-11
    • 2010-11-13
    • 2011-06-20
    • 1970-01-01
    • 2011-08-25
    • 1970-01-01
    相关资源
    最近更新 更多