0

I am setting up a MapView annotation when my map view appears with the following code, which all seems to be working fine except the selection:

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    MapAnnotation *pinpoint = [[MapAnnotation alloc] initWithTitle:@"Title" andCoordinate:loc];

    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 1500, 1500);
    [self.mapView setRegion:region animated:true];
    [self.mapView addAnnotation:pinpoint];
    [self.mapView selectAnnotation:pinpoint animated:true];  //nothing happens!?   
}

But if call the same method in this callback, it works fine:

- (void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views
{
    MKAnnotationView *annotationView = [views objectAtIndex:0];
    id <MKAnnotation> mp = [annotationView annotation];
    [mv selectAnnotation:mp animated:YES];
}

What is causing this? Is this just not the appropriate place to call selectAnnotation? Do I have to wait until after viewForAnnotation has been called?

Bryan
  • 8,748
  • 7
  • 41
  • 62

1 Answers1

2

The annotation view has to have been added before you can select the annotation.

Please see this popular question:

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

Community
  • 1
  • 1
TomSwift
  • 39,369
  • 12
  • 121
  • 149