首页 > 解决方案 > Apple MapKit:地图上的蓝点(用户位置)标记下未显示注释

问题描述

我正在使用 Mapkit 创建一个应用程序,用户可以在其中在地图上放置点标记(MKPointAnnotation)。问题是:似乎在蓝点(用户位置指示器)附近没有绘制注释。

这是我的代码片段。在这个片段中,每当拖动地图时,我只需将点标记放在地图的中心。这在大多数情况下都有效,除非蓝点也在地图中间,此时点标记不会在地图上绘制

override func viewDidLoad() {
    super.viewDidLoad()
    mapView = MKMapView(frame:self.view.frame)
    mapView.delegate = self
    mapView.mapType = .standard
    mapView.isZoomEnabled = true
    mapView.isScrollEnabled = true
    mapView.isRotateEnabled = true
    mapView.showsUserLocation = true
    mapView.userTrackingMode = .follow
    mapView.isUserInteractionEnabled = true
    mapView.mapType = MKMapType.satellite
    view.addSubview(mapView)
}

func mapViewDidChangeVisibleRegion(_ mapView: MKMapView) {
    updateLocationMarker(coordinate: mapView.centerCoordinate, showTitle: false)
}

func updateLocationMarker(coordinate: CLLocationCoordinate2D, showTitle: Bool) {
    if self.pointMarker != nil {
        mapView.removeAnnotation(pointMarker)
    }
    self.pointMarker = MKPointAnnotation()
    self.pointMarker.coordinate = coordinate
    self.pointMarker.title = stringFromCoordinate(coordinate: coordinate)
    self.mapView.addAnnotation(pointMarker)
}

看来“蓝点”是一种特殊类型的 MKAnnotation。我尝试设置视图的显示优先级,但它们似乎没有做任何事情:

    let pointMarkerView = self.mapView.view(for: pointMarker)
    pointMarkerView?.displayPriority = .required
    let userLocationView = self.mapView.view(for: mapView.userLocation)
    userLocationView?.displayPriority = .defaultLow

任何帮助将非常感激!

标签: iosswiftmapkitmapkitannotation

解决方案


推荐阅读