MKMapView overlapping MKAnnotations only allow showing two of the callouts
Asked Answered
M

1

1

I have three MKPointAnnotation objects that are added in the exact same coordinates to an MKMapView and they are each given a unique title.

MKPointAnnotation *a1 = [[MKPointAnnotation alloc] init];
a1.coordinate = CLLocationCoordinate2DMake(45.875684, -122.656254);
a1.title = @"Title 1";
[self.mapView addAnnotation:a1];

MKPointAnnotation *a2 = [[MKPointAnnotation alloc] init];
a2.coordinate = CLLocationCoordinate2DMake(45.875684, -122.656254);
a2.title = @"Title 2";
[self.mapView addAnnotation:a2];

MKPointAnnotation *a3 = [[MKPointAnnotation alloc] init];
a3.coordinate = CLLocationCoordinate2DMake(45.875684, -122.656254);
a3.title = @"Title 3";
[self.mapView addAnnotation:a3];

When adding them to the map, normally you can just tap the annotation, then tap again and again to cycle through the overlapped pins. However, when there are more than two, it seems you are limited to only cycling through two of the annotations, even though there are three that are overlapping. Title 1 is never shown:

enter image description here

This seems like an MKMapView bug to me, but has anyone else experienced this before and found a way around it?

Meleager answered 8/8, 2017 at 4:10 Comment(3)
Handling MKMapView Annotation Pins on the Same Coordinate with Swift blog.stormid.com/2015/10/mkmapview-pins-swiftPad
@KosukeOgawa This wouldn't be helpful as our app must have pins placed at the exact coordinates.Meleager
Ohh I get it. In iOS 11 and later, You can use MKAnnotationView. clusteringIdentifier ... Good Luck! i.sstatic.net/2CLhj.gifPad
O
0

I can confirm this works as you describe and it does seem like a bug (or at least I couldn't find any relevant documentation).

One solution (workaround) would be to manage the two annotations that the mapView can handle by yourself. Then, when the user selects (or deselects) an annotation, you can swap the one that was just deselected to the 3rd one. Something like:

extension ViewController: MKMapViewDelegate {
    func mapView(_ mapView: MKMapView, didDeselect view: MKAnnotationView) {
        mapView.removeAnnotation(view.annotation!)
        mapView.addAnnotation(a1)
    }
}

I hope this makes sense. Good luck!

Olnek answered 8/8, 2017 at 9:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.