Why doesn't this work?
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
if (TRACE_LOG) NSLog(@"%s", __FUNCTION__);
[mapView selectAnnotation:[views lastObject] animated:YES];
return;
}
Thanks, Z@K!
Why doesn't this work?
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
if (TRACE_LOG) NSLog(@"%s", __FUNCTION__);
[mapView selectAnnotation:[views lastObject] animated:YES];
return;
}
Thanks, Z@K!
Because you must select annotation object, not the view that corresponds to it.
I'm 100% not sure, but I think the following should work:
MKAnnotationView* annotationView = (MKAnnotationView*)[views lastObject];
[mapView selectAnnotation:annotationView.annotation animated:YES];
If you store your annotations somewhere it might be better to get annotation object you need directly from that storage. Note that all these methods have affect only if you try to select annotation that is currently visible on screen.
If you only have one annotation, this is the easiest way:
[mapView selectAnnotation:[mapView.annotations objectAtIndex:0] animated:true];
Note: if you have the pin drop animation enabled, this disables it. Basically, it selects the annotation as soon as its added to the map view so it cuts off the animation.
If you want to wait for the pin drop animation to finish before selecting it, heres a semi-hacky way to make it happen. First, make sure your view controller is set up as the MKMapViewDelegate then:
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:.7 target:self selector:@selector(pinDropped) userInfo:nil repeats:NO];
}
- (void) pinDropped {
[mapView selectAnnotation:[mapView.annotations objectAtIndex:0] animated:true];
}