Skip to content

Instantly share code, notes, and snippets.

@afarnham
Created May 5, 2010 18:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save afarnham/391205 to your computer and use it in GitHub Desktop.
Save afarnham/391205 to your computer and use it in GitHub Desktop.
MKCoordinateRegion region;
if ([locations count] > 0) {
CLLocation *firstLoc = [locations objectAtIndex:0];
CLLocationCoordinate2D southWest = firstCoord.coordinate;
CLLocationCoordinate2D northEast = southWest;
for (CLLocation* loc in founds) {
southWest.latitude = MIN(southWest.latitude, loc.coordinate.latitude);
southWest.longitude = MIN(southWest.longitude, loc.coordinate.longitude);
northEast.latitude = MAX(northEast.latitude, loc.coordinate.latitude);
northEast.longitude = MAX(northEast.longitude, loc.coordinate.longitude);
}
CLLocation *locSouthWest = [[CLLocation alloc] initWithLatitude:southWest.latitude longitude:southWest.longitude];
CLLocation *locNorthEast = [[CLLocation alloc] initWithLatitude:northEast.latitude longitude:northEast.longitude];
// This is a diag distance (if you wanted tighter you could do NE-NW or NE-SE)
CLLocationDistance meters = [locSouthWest getDistanceFrom:locNorthEast];
region.center.latitude = (southWest.latitude + northEast.latitude) / 2.0;
region.center.longitude = (southWest.longitude + northEast.longitude) / 2.0;
region.span.latitudeDelta = meters / 111319.5;
region.span.longitudeDelta = 0.0;
[locSouthWest release];
[locNorthEast release];
} else {
region.center.latitude = DEFAULT_LAT;
region.center.longitude = DEFAULT_LONG;
region.span.latitudeDelta = 0.1;
region.span.longitudeDelta = 0.1;
}
self.mapView.region = [self.mapView regionThatFits:region];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment