MapKit iOS 16: MKMarkerAnnotationView maker hides image; .markerTintColor = UIColor.clear doesn't work

859 Views Asked by At

This is how MKMarkerAnnotationView annotations look in iOS 15 in my app.

The markers on the beach only consist of an image no bubble from the MKMarkerAnnotationView

AnnotationView consisting only of images

With iOS 16 beta 3, many but not all images are hidden by the marker bubble; colors appear to be random.

AnnotationView images hidden by bubbles

MKMarkerAnnotationView is set like this:

self.glyphImage = myImage
self.glyphText = ""
self.glyphTintColor = UIColor.clear
self.markerTintColor = UIColor.clear

I checked in the debugger that this code is executed.

What is the cause and how can I prevent the bubbles from hiding the image?

3

There are 3 best solutions below

0
Gerd Castan On

The colors of the bubbles are colors that are used elsewhere in the app.

The following workaround fixes the problem:

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

replace mapView.dequeueReusableAnnotationView(withIdentifier: identifier) and create a fresh instance of MKMarkerAnnotationView or your subclass of MKMarkerAnnotationView.

This is a bad workaround since mapView.dequeueReusableAnnotationView(withIdentifier: identifier) is there for a reason, the workaround should have worse performance.

The nature of the workaround suggests a bug in Apples implementation that the internal representation of the bubble is created only once or created only once if UIColor.clear is used.

Let's hope Apple fixes this until iOS 16 or someone comes up with a better answer.

App with workaround and iOS 16 beta 3:

Fixed OS 16 version

Be aware that this workaround only helps in the case where the bubble always is invisible. It does not help in the case if you want the bubble sometimes visible and sometimes invisible vor the same annotation.

0
Shengpan On

My application primary feature is map and occur this problem too. The perfect solution that I found is set different identifier for same view class with different annotation type.

Example:

mapView.register(ERMarkerAnnotationView.self, forAnnotationViewWithReuseIdentifier: "BubbleType")
mapView.register(ERMarkerAnnotationView.self, forAnnotationViewWithReuseIdentifier: "NonBubbleType")

The use dequeueReusableAnnotationView(withIdentifier:for:) to dequeue different type annotation view in different case.

if needBubble {
    view = mapView.dequeueReusableAnnotationView(withIdentifier: "BubbleType",
                                                         for: anno) as? ERMarkerAnnotationView
} else {
    view = mapView.dequeueReusableAnnotationView(withIdentifier: "NonBubbleType",
                                                         for: anno) as? ERMarkerAnnotationView
}
1
Grant Kamin On

In iOS 16 every time you set the MKAnnotationView image, the glyph resets. So every time you update your image you also need to clear the glyph.

image = [Your Image Here]
glyphTintColor = .clear
glyphText = ""
markerTintColor = .clear