Select Annotation and Center MKMapView Around
Posted: October 26th, 2010 | Author: admin | Filed under: Objective-C | No Comments »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.
-
-
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views{
-
for (MKAnnotationView *view in views){
-
// doing normal stuff HERE
-
if ([view annotation] == selectedAnnotation){
-
[self performSelector:@selector(openCallout:)
-
withObject:[view annotation]
-
afterDelay:0.5];
-
} // end if
-
} // end for
-
}
-
-
- (void)openCallout:(id <MKAnnotation>)annotation {
-
CLLocationDistance distance = REGIONZOOM;
-
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance( annotation.coordinate, distance, distance);
-
[mapCordoba setRegion:region animated:YES];
-
[self.mapCordoba deselectAnnotation:annotation
-
animated:NO];
-
[self.mapCordoba selectAnnotation:annotation
-
animated:NO];
-
}
-
Leave a Reply