问题
After the authorization checks, to obtain the user location, I am calling this CLLocation delegate function:
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let latestLocation = locations.first else { return }
if currentCoordinate == nil {
zoomToLocation(with: latestLocation.coordinate)
}
currentCoordinate = latestLocation.coordinate
}
And then I zoom onto the user's location:
func zoomToLocation(with coordinate: CLLocationCoordinate2D) {
let regionDimension : Double = 1000
let zoomRegion = MKCoordinateRegion.init(center: coordinate, latitudinalMeters: regionDimension, longitudinalMeters: regionDimension)
mapView.setRegion(zoomRegion, animated: true)
}
So far so good. But I am loading some "destinations" for which I have custom annotation so I need to call this delegate function:
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
}
And it replaces the blue dot for the user location with the same custom marker. This is a really weird user experience. How do I exclude "my location" from getting the custom annotation?
回答1:
The problem is that you are not examining the incoming annotation
. Examine it! If it is a MKUserLocation, return nil
.
来源:https://stackoverflow.com/questions/61785563/how-to-set-a-custom-annotations-for-all-points-except-for-user-location