【发布时间】: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