Skip to content

Instantly share code, notes, and snippets.

@xavierjurado
Created November 21, 2013 15:33
Show Gist options
  • Save xavierjurado/7583725 to your computer and use it in GitHub Desktop.
Save xavierjurado/7583725 to your computer and use it in GitHub Desktop.
Helper function to calculate the region needed to show a number of POIs
// Helper function to calculate the region needed to show a number of POIs
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;
}
@xavierjurado
Copy link
Author

records must contain objects key value coding-compliant for keys "longitude" and "latitude"

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