如何为除用户位置以外的所有点设置自定义注释?

How to set a custom annotations for all points except for user location?

授权检查后,为了获取用户位置,我调用了这个 CLLocation 委托函数:

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

        guard let latestLocation = locations.first else { return }

        if currentCoordinate == nil {
            zoomToLocation(with: latestLocation.coordinate)
        }
        currentCoordinate = latestLocation.coordinate
    }

然后我放大到用户的位置:

func zoomToLocation(with coordinate: CLLocationCoordinate2D) {
        let regionDimension : Double = 1000
        let zoomRegion = MKCoordinateRegion.init(center: coordinate, latitudinalMeters: regionDimension, longitudinalMeters: regionDimension)
        mapView.setRegion(zoomRegion, animated: true)
    }

到目前为止一切顺利。但是我正在加载一些 "destinations" 我有自定义注释所以我需要调用这个委托函数:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {

        var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "AnnotationView")

        if annotationView == nil {
            annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "AnnotationView")
        }

        annotationView?.image = UIImage(named: "icon_marker")
        annotationView?.canShowCallout = true
        return annotationView
    }

并且它用相同的自定义标记替换了用户位置的蓝点。这是一个非常奇怪的用户体验。如何排除 "my location" 获取自定义注释?

问题是您没有检查传入的 annotation。检查一下!如果是 MKUserLocation,return nil.