Skip to content

Instantly share code, notes, and snippets.

@JigsChanchiya
Created June 22, 2013 13:23
Show Gist options
  • Save JigsChanchiya/5840849 to your computer and use it in GitHub Desktop.
Save JigsChanchiya/5840849 to your computer and use it in GitHub Desktop.
Get map view region based on all annotation pin for screen fit size zoom.
-(MKCoordinateRegion) regionForAnnotations:(NSMutableArray *)annotations
{
double minLat=90.0f, maxLat=-90.0f;
double minLon=180.0f, maxLon=-180.0f;
for (id<MKAnnotation> mka in annotations) {
if ( mka.coordinate.latitude < minLat ) minLat = mka.coordinate.latitude;
if ( mka.coordinate.latitude > maxLat ) maxLat = mka.coordinate.latitude;
if ( mka.coordinate.longitude < minLon ) minLon = mka.coordinate.longitude;
if ( mka.coordinate.longitude > maxLon ) maxLon = mka.coordinate.longitude;
}
CLLocationCoordinate2D center = CLLocationCoordinate2DMake((minLat+maxLat)/2.0, (minLon+maxLon)/2.0);
MKCoordinateSpan span = MKCoordinateSpanMake((maxLat-minLat)+0.5f, (maxLon-minLon)+0.5f);
MKCoordinateRegion region = MKCoordinateRegionMake (center, span);
return region;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment