Skip to content

Instantly share code, notes, and snippets.

@aaronpk
Created September 25, 2013 00:54
Show Gist options
  • Save aaronpk/6693579 to your computer and use it in GitHub Desktop.
Save aaronpk/6693579 to your computer and use it in GitHub Desktop.
Center a MKMapView given a GeoJSON bounding box
double lng1 = [bbox[0] doubleValue];
double lat1 = [bbox[1] doubleValue];
double lng2 = [bbox[2] doubleValue];
double lat2 = [bbox[3] doubleValue];
MKCoordinateSpan span;
span.latitudeDelta = fabs(lat2 - lat1);
span.longitudeDelta = fabs(lng2 - lng1);
CLLocationCoordinate2D center;
center.latitude = fmax(lat1, lat2) - (span.latitudeDelta / 2.0);
center.longitude = fmax(lng1, lng2) - (span.longitudeDelta / 2.0);
MKCoordinateRegion region;
region.span = span;
region.center = center;
[self.mapView setRegion:region animated:NO];
@joeyhagedorn
Copy link

Hey, just ran across this while working on a project. I need to go the opposite direction, but this was a nice help. Thanks Aaron!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment