Select Annotation and Center MKMapView Around

When adding annotations to MKMapView it is nice to center the map around a particular selected annotation.
I have found many people on the net wishing to do that and having great difficulties. After struggling for a day I found a good solution to this problem: wait half a second after all the annotations have been added to the map before going ahead and select an annotation and center the map around.

This particular action has to be done at the end of the didAddAnnotationView function for having the desire effect.

  1.  
  2. - (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views{
  3.         for (MKAnnotationView *view in views){
  4.         // doing normal stuff HERE
  5.         if ([view annotation] == selectedAnnotation){
  6.         [self performSelector:@selector(openCallout:)
  7.                 withObject:[view annotation]
  8.                 afterDelay:0.5];
  9.         } // end if
  10.        } // end for
  11. }
  12.  
  13. - (void)openCallout:(id <MKAnnotation>)annotation {
  14. CLLocationDistance distance = REGIONZOOM;
  15. MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance( annotation.coordinate, distance, distance);
  16.         [mapCordoba setRegion:region animated:YES];
  17.         [self.mapCordoba deselectAnnotation:annotation
  18.                                      animated:NO];
  19.         [self.mapCordoba selectAnnotation:annotation
  20.                                      animated:NO];
  21. }
  22.  

Post to Twitter