Skip to content

Instantly share code, notes, and snippets.

@fmundaca
Last active December 11, 2015 16:18
Show Gist options
  • Save fmundaca/4626559 to your computer and use it in GitHub Desktop.
Save fmundaca/4626559 to your computer and use it in GitHub Desktop.
#define MINIMUM_ZOOM_ARC 0.003 //approximately 1 miles (1 degree of arc ~= 69 miles)
#define ANNOTATION_REGION_PAD_FACTOR 1.1
#define MAX_DEGREES_ARC 360
+(MKCoordinateRegion)adjustZoomMapView:(NSArray*) locations {
int count = [locations count];
MKMapPoint points[count]; //C array of MKMapPoint struct
for( int i=0; i<count; i++ ) //load points C array by converting coordinates to points
{
CLLocationCoordinate2D *point = [locations objectAtIndex:i];
points[i] = MKMapPointForCoordinate(coordinate);
}
MKMapRect mapRect = [[MKPolygon polygonWithPoints:points count:count] boundingMapRect];
MKCoordinateRegion region = MKCoordinateRegionForMapRect(mapRect);
region.span.latitudeDelta *= ANNOTATION_REGION_PAD_FACTOR;
region.span.longitudeDelta *= ANNOTATION_REGION_PAD_FACTOR;
if( region.span.latitudeDelta > MAX_DEGREES_ARC ) { region.span.latitudeDelta = MAX_DEGREES_ARC; }
if( region.span.longitudeDelta > MAX_DEGREES_ARC ){ region.span.longitudeDelta = MAX_DEGREES_ARC; }
if( region.span.latitudeDelta < MINIMUM_ZOOM_ARC ) { region.span.latitudeDelta = MINIMUM_ZOOM_ARC; }
if( region.span.longitudeDelta < MINIMUM_ZOOM_ARC ) { region.span.longitudeDelta = MINIMUM_ZOOM_ARC; }
return region;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment