Created
April 24, 2014 04:42
-
-
Save akozlik/11241740 to your computer and use it in GitHub Desktop.
Zoom MapKit out to fit pins
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-(void)zoomToFitMapAnnotations:(MKMapView*)mapView | |
{ | |
if([mapView.annotations count] == 0) | |
return; | |
CLLocationCoordinate2D topLeftCoord; | |
topLeftCoord.latitude = -90; | |
topLeftCoord.longitude = 180; | |
CLLocationCoordinate2D bottomRightCoord; | |
bottomRightCoord.latitude = 90; | |
bottomRightCoord.longitude = -180; | |
for(MojoLocationMapItem* annotation in mapView.annotations) | |
{ | |
topLeftCoord.longitude = fmin(topLeftCoord.longitude, annotation.coordinate.longitude); | |
topLeftCoord.latitude = fmax(topLeftCoord.latitude, annotation.coordinate.latitude); | |
bottomRightCoord.longitude = fmax(bottomRightCoord.longitude, annotation.coordinate.longitude); | |
bottomRightCoord.latitude = fmin(bottomRightCoord.latitude, annotation.coordinate.latitude); | |
} | |
MKCoordinateRegion region; | |
region.center.latitude = topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) * 0.5; | |
region.center.longitude = topLeftCoord.longitude + (bottomRightCoord.longitude - topLeftCoord.longitude) * 0.5; | |
region.span.latitudeDelta = fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 1.3; // Add a little extra space on the sides | |
region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.3; // Add a little extra space on the sides | |
region = [mapView regionThatFits:region]; | |
[mapView setRegion:region animated:YES]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment