Skip to content

Instantly share code, notes, and snippets.

@rais38
Created February 1, 2013 10:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rais38/4690624 to your computer and use it in GitHub Desktop.
Save rais38/4690624 to your computer and use it in GitHub Desktop.
Calculate the region needed to show a number of POIs in MKMapView
/**
MKCoordinateRegion viewRegion = [self RegionForAnnotations:_arrayPois];
[_mapView setRegion:viewRegion animated:YES];
*/
- (MKCoordinateRegion)RegionForAnnotations:(NSArray *)records {
MKCoordinateRegion region;
// center the map arround our records
// @see https://devforums.apple.com/message/48525#48525
double minLatitude = [[records valueForKeyPath:@"@min.latitude"] doubleValue];
double maxLatitude = [[records valueForKeyPath:@"@max.latitude"] doubleValue];
double minLongitude = [[records valueForKeyPath:@"@min.longitude"] doubleValue];
double maxLongitude = [[records valueForKeyPath:@"@max.longitude"] doubleValue];
region.center.latitude = (maxLatitude - minLatitude)/2.0 + minLatitude;
region.center.longitude = (maxLongitude - minLongitude)/2.0 + minLongitude;
region.span.latitudeDelta = (maxLatitude - minLatitude);
region.span.longitudeDelta = (maxLongitude - minLongitude);
region.span.latitudeDelta = MAX (region.span.latitudeDelta, 0.03);
region.span.longitudeDelta = MAX (region.span.longitudeDelta, 0.03);
return region;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment