MKMapView overlapping MKAnnotations only allow showing two of the callouts

旧巷老猫 提交于 2019-12-11 05:58:00

问题


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:

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


回答1:


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!



来源:https://stackoverflow.com/questions/45559152/mkmapview-overlapping-mkannotations-only-allow-showing-two-of-the-callouts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!