MapKit multiple pins same coordinates, different info selection

不想你离开。 提交于 2019-12-04 10:17:21

You can implement the didSelectAnnotationView delegate method and select the "correct" annotation yourself depending on what the last "correct" selection was.

If you only have these annotations on the map and only one cluster of them, then you can keep one int ivar that remembers what the last selected annotation was and increment it in the delegate method.

For example:

// In .h
int lastAnnotationSelected;

// In .m
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
    int nextAnnotationToSelect = (lastAnnotationSelected + 1) 
                                     % mapView.annotations.count;

    id<MKAnnotation> nextAnnotation =
        [mapView.annotations objectAtIndex:nextAnnotationToSelect];

    [mapView selectAnnotation:nextAnnotation animated:YES];

    lastAnnotationSelected = nextAnnotationToSelect;
}

If you also have showsUserLocation turned on, then you'll have to add a check for MKUserLocation in that method and skip it (if you want to) and go to the next annotation in the cluster.

Also, if you have multiple clusters of annotations (3 at coordinate A, 5 at coordinate B, 4 at coordinate C, etc), then you'll need to keep track of an array of lastAnnotationSelected ints and in the method, first determine what cluster was selected and get the next annotation to select in that cluster.

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