Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trigger MKAnnotationView's callout view without touching the pin?

Tags:

iphone

I'm working on a MKMapView with the usual colored pin as the location points. I would like to be able to have the callout displayed without touching the pin.

How should I do that? Calling setSelected:YES on the annotationview did nothing. I'm thinking of simulate a touch on the pin but I'm not sure how to go about it.

like image 735
Teo Choong Ping Avatar asked Jun 11 '09 01:06

Teo Choong Ping


4 Answers

But there is a catch to get benvolioT's solution to work, the code

for (id<MKAnnotation> currentAnnotation in mapView.annotations) {       
    if ([currentAnnotation isEqual:annotationToSelect]) {
        [mapView selectAnnotation:currentAnnotation animated:FALSE];
    }
}

should be called from - (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView, and nowhere else.

The sequence in which the various methods like viewWillAppear, viewDidAppear of UIViewController and the - (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView is called is different between the first time the map is loaded with one particular location and the subsequent times the map is displayed with the same location. This is a bit tricky.

like image 170
Steve Shi Avatar answered Nov 15 '22 00:11

Steve Shi


Ok, here's the solution to this problem.

To display the callout use MKMapView's selectAnnotation:animated method.

like image 34
Teo Choong Ping Avatar answered Nov 15 '22 00:11

Teo Choong Ping


Assuming that you want the last annotation view to be selected, you can put the code below:

[mapView selectAnnotation:[[mapView annotations] lastObject] animated:YES];

in the delegate below:

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
{
    //Here
    [mapView selectAnnotation:[[mapView annotations] lastObject] animated:YES];
}
like image 32
user507778 Avatar answered Nov 15 '22 02:11

user507778


Ok, to successfully add the Callout you need to call selectAnnotation:animated after all the annotation views have been added, using the delegate's didAddAnnotationViews:

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views{
    for (id<MKAnnotation> currentAnnotation in mapView.annotations) {       
        if ([currentAnnotation isEqual: annotationToSelect]) {
            [mapView selectAnnotation:currentAnnotation animated:YES];
        }
    }
}
like image 21
Vespassassina Avatar answered Nov 15 '22 02:11

Vespassassina