MapKit Annotations Disappearing
The accepted answer from Leszek Szary is correct.
But there is some fineprint. Sometimes MKMarkerAnnotationView
s are not rendered, even if
view.displayPriority = .required
is set.
What you are seeing is a combination of different rules.
MKAnnotationViews
are rendered from top to bottom of the map. (It doesn't matter where north is).- If
MapKit
decides to draw overlappingMKAnnotationViews
, then theMKAnnotationView
nearer to the bottom is drawn on top (because it's drawn later) - Not only
MKAnnotationViews
, also titles rendered belowMKMArkerAnnotationViews
need space. The rendering of those titles is influenced bymarkerView.titleVisibility
. IfmarkerView.titleVisibility
is set to.visible
(instead of the default.adaptive
), then this title is stronger than aMarkerAnnotationView
that is rendered later, even if the laterMarkerAnnotationView
has adisplayPriority = .required
. TheMarkerAnnotationView
nearer to the bottom is not rendered. - This even happens if the
MarkerAnnotationView
nearer to the top has a lowdisplayPriority
. So aMarkerAnnotationView
with lowdisplayPriority
and.titleVisibility = .visible
can make aMarkerAnnotationView
nearer to the bottom withdisplayPriority = .required
disappear.
I am not aware of a documentation of this behaviour. This is the result of my experiments with iOS 12. My description is sipmplified.
NiltiakSivad's solution works but it reverts to the old iOS 10 look. If you want to keep the new iOS 11 balloon markers for iOS 11 and use the old pin look only for older iOS versions then you can implement the delegate method as below:
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
let reuseIdentifier = "annotationView"
var view = mapView.dequeueReusableAnnotationView(withIdentifier: reuseIdentifier)
if #available(iOS 11.0, *) {
if view == nil {
view = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier)
}
view?.displayPriority = .required
} else {
if view == nil {
view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier)
}
}
view?.annotation = annotation
view?.canShowCallout = true
return view
}