MapKit multiple pins same coordinates, different info selection

家住魔仙堡 提交于 2019-12-06 06:39:32

问题


I have the following situation: - 3 pins with same coordinates but different title and info - on map there is ony one pin

It is possible to tap multiple times on that pin and the annotation displayed to be: - first tap -> the annotation for pin 1 - second tap -> the annotation for pin 2 - third tap -> the annotation for pin 3 - fourth tap -> the annotation for pin 1

Do you have any ideas how should I implement it?


回答1:


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.



来源:https://stackoverflow.com/questions/6608349/mapkit-multiple-pins-same-coordinates-different-info-selection

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