问题
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