Mapkit, how to detect annotations have loaded

2020-03-26 06:04发布

问题:

I want the annotations callout to popup when the pin has finished it's drop animation. Currently I am able to simulate it with the following method:

- (void)showCallOut {
    [myMapView selectAnnotation:[myMapView.annotations objectAtIndex:0] animated:YES];
}

In my viewDidLoad is where my annotation is created

    [myMapView addAnnotation:annotation];

The problem is that you simply can't callout [self showCallOut]; after that because at run time it responds before MapKit has "acknowledged" the annotation drop. I need to either create a delay (Would like to avoid this) or find the proper way to detect when annotations are in place and then run the showCallOut method.

Thanks for any help!

Thanks to aBitObvious below for providing a solution:

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
    [self performSelector:@selector(showCallOut) withObject:nil afterDelay:1];
}

回答1:

Try using the didAddAnnotationViews delegate method:

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
{
    [self showCallOut];
}

Make sure your map view's delegate is set.

Edit:
If you need to add a delay regardless, then try this instead (example with 1/2 second delay):

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
{
    [self performSelector:@selector(showCallOut) withObject:nil afterDelay:0.5];
}