// pad our map by 10% around the farthest annotations
#define MAP_PADDING 1.1
// we'll make sure that our minimum vertical span is about a kilometer
// there are ~111km to a degree of latitude. regionThatFits will take care of
// longitude, which is more complicated, anyway.
#define MINIMUM_VISIBLE_LATITUDE 0.01
- (void)mapZoom
{
CLLocationDegrees minLatitude = 90.0;
CLLocationDegrees maxLatitude = -90.0;
CLLocationDegrees minLongitude = 180.0;
CLLocationDegrees maxLongitude = -180.0;
for (MapPoint *p in [self.mapView annotations]) {
if (p.coordinate.latitude < minLatitude)
minLatitude = p.coordinate.latitude;
if (p.coordinate.latitude > maxLatitude)
maxLatitude = p.coordinate.latitude;
if (p.coordinate.longitude < minLongitude)
minLongitude = p.coordinate.longitude;
if (p.coordinate.longitude > maxLongitude)
maxLongitude = p.coordinate.longitude;
}
MKCoordinateRegion region;
region.center.latitude = (minLatitude + maxLatitude) / 2;
region.center.longitude = (minLongitude + maxLongitude) / 2;
region.span.latitudeDelta = (maxLatitude - minLatitude) * MAP_PADDING;
region.span.latitudeDelta = (region.span.latitudeDelta < MINIMUM_VISIBLE_LATITUDE) ? MINIMUM_VISIBLE_LATITUDE : region.span.latitudeDelta;
region.span.longitudeDelta = (maxLongitude - minLongitude) * MAP_PADDING;
MKCoordinateRegion scaledRegion = [self.mapView regionThatFits:region];
[self.mapView setRegion:scaledRegion animated:YES];
}