I am trying to implement annotation onto my mapkit I am using
mapView.register(EventMarkerView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)
However, I often see this sort of format
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
guard let annotation = annotation as? Event else { return nil }
let identifier = "marker"
var view: MKMarkerAnnotationView
if let dequeuedView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
as? MKMarkerAnnotationView {
dequeuedView.annotation = annotation
view = dequeuedView
} else {
view = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: identifier)
view.canShowCallout = true
view.calloutOffset = CGPoint(x: -5, y: 5)
view.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
}
return view
}
Would I use the latter form if I had more than 1 type of annotation?
I am also curious if my MKMapViewDefaultAnnotationViewReuseIdentifier
is valid? I did not choose it, but since I only have 1 annotation type, it shouldn't matter, correct?
Finally, does the reuse identifier still have the functionality of the dequeueReusableAnnotationView(withIdentifier: identifier)
?
There are many things to consider here:
MKMapViewDefaultAnnotationViewReuseIdentifier
is only supported on iOS 11+, so if you want to target an earlier iOS you might want to use the latter form.Bottom line:
if only targeting iOS 11+ and only have 1 annotation you are fine with MKMapViewDefaultAnnotationViewReuseIdentifier
. Otherwise consider the other form.
Hope this helps you make a decision.