Zoom MKMapView to an MKAnnotationView's frame
ios, mapkit, objective-c, xcode
Solution
Start by removing the group pin
[mapView removeAnnotation:groupAnnotation];
Then add the pins in the cluster
[mapView addAnnotations:clusterAnnotations];
Then determine the region to zoom to
CLLocationDegrees minLat = 90;
CLLocationDegrees maxLat = -90;
CLLocationDegress minLong = 180;
CLLocationDegrees maxLong = -180
[clusterAnnotations enumerateUsingBlock:^(id<MKAnnotation> annotation, NSUInteger idx, BOOL *stop) {
CLLocationCoordinate2D coordinate = annotation.coordinate;
minLat = MIN(minLat, coordinate.latitude);
maxLat = MAX(maxLat, coordinate.latitude);
minLong = MIN(minLong, coordinate.longitude);
maxLong = MAX(maxLong, coordinate.longitude);
}
CLLocationCoordinate2D center = CLLocationCoordinate2DMake((minLat + maxLat)/2.f, (minLong + maxLong)/2.f);
MKCoordinateSpan span = MKCoordinateSpanMake((maxLat - minLat)*1.25, (maxLong - minLong)*1.25); //1.25 is for padding
MKCoordinateRegion region = MKCoordinateRegionMake(center, span);
[mapView setRegion:[mapView regionThatFits:region] animated:YES];
Problem
I have a grouped pin that I want to zoom in on. Of course I know the coords of the pin, and have it's view rect. I just want to zoom the map to just the right region so that the cluster fully expands showing all pins (plus a bit of padding). What's a good way of doing this? Sidenote: In my setup the cluster pin will automatically be expanded into individual pins when the zoom level increases so I'm good there. What I need to know is how to set the MapView to a new region based on the frame and coords of the cluster pin.