5

My app places a pushpin on the map and then selects its using animation so the user has a visual clue and can immediately read the title/subtitle. The following code works in both iOS4 and iOS5, but in iOS5, the annotation doesn't get selected automatically unless I change the animation to NO in the selectAnnotation method.

Any ideas why?

MapAnnotations *pushpin = [[MapAnnotations alloc] initWithCoordinate:coordinate];
pushpin.title = [selectedStation valueForKey:@"name"];
pushpin.subtitle = [selectedStation valueForKey:@"address"];
[stationMap addAnnotation:pushpin];
[stationMap selectAnnotation:pushpin animated:YES];

[pushpin release]; pushpin = nil;
afterxleep
  • 622
  • 1
  • 5
  • 17

1 Answers1

5

Not sure why it would work before but the animation probably requires the annotation view to be created and ready which is unlikely immediately after adding the annotation.

What you can do is move the selection to the didAddAnnotationViews delegate method which should work on all iOS versions:

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
{
    for (MKAnnotationView *av in views) {
        if ([av.annotation isKindOfClass:[MapAnnotations class]]) {
            MapAnnotations *pushpin = (MapAnnotations *)av.annotation;
            if (_this_pushpin_is_the_one_to_select) {
                [mapView selectAnnotation:av.annotation animated:YES];
                break;  //or return;
            }
        }
    }
}
  • Thanks. They must have changed something in iOS5 regarding animations in annotations. This works. A bit too much code for such a simple thing, but does the job. – afterxleep Oct 26 '11 at 14:10
  • 2
    To avoid the search, you could keep a ref to the selected annotation as an ivar when you add it so you just need to call selectAnnotation in the delegate. –  Oct 26 '11 at 14:15