Skip to content

Instantly share code, notes, and snippets.

@MaciejGad
Forked from robmooney/gist:923301
Last active August 29, 2015 14:01
Show Gist options
  • Save MaciejGad/45297015683346275463 to your computer and use it in GitHub Desktop.
Save MaciejGad/45297015683346275463 to your computer and use it in GitHub Desktop.
// returns a MKCoordinateRegion that encompasses an array of MKAnnotations
- (MKCoordinateRegion)regionForAnnotations:(NSArray *)annotations {
CLLocationDegrees minLat = 90.0;
CLLocationDegrees maxLat = -90.0;
CLLocationDegrees minLon = 180.0;
CLLocationDegrees maxLon = -180.0;
for (id <MKAnnotation> annotation in annotations) {
if (annotation.coordinate.latitude < minLat) {
minLat = annotation.coordinate.latitude;
}
if (annotation.coordinate.longitude < minLon) {
minLon = annotation.coordinate.longitude;
}
if (annotation.coordinate.latitude > maxLat) {
maxLat = annotation.coordinate.latitude;
}
if (annotation.coordinate.longitude > maxLon) {
maxLon = annotation.coordinate.longitude;
}
}
//change borders to show all annotations
maxLat += 0.01;
minLat -= 0.01;
maxLon += 0.01;
minLon -= 0.01;
MKCoordinateSpan span = MKCoordinateSpanMake(maxLat - minLat, maxLon - minLon);
CLLocationCoordinate2D center = CLLocationCoordinate2DMake((maxLat - span.latitudeDelta / 2), maxLon - span.longitudeDelta / 2);
return MKCoordinateRegionMake(center, span);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment