Skip to content

Instantly share code, notes, and snippets.

@JigarM
Created January 5, 2015 06:56
Show Gist options
  • Save JigarM/59a3feaa46b7e5624338 to your computer and use it in GitHub Desktop.
Save JigarM/59a3feaa46b7e5624338 to your computer and use it in GitHub Desktop.
Zooms out a MKMapView to enclose all its annotations (inc. current location)
//Original gist : https://gist.github.com/andrewgleave/915374
-(void)MKMapViewFitAnnotations {
MKMapRect zoomRect = MKMapRectNull;
for (id <MKAnnotation> annotation in self.mkmapview.annotations) {
MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
//MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0);
MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0.1, 0.1);
if (MKMapRectIsNull(zoomRect)) {
zoomRect = pointRect;
} else {
zoomRect = MKMapRectUnion(zoomRect, pointRect);
}
}
//[self.mapView setVisibleMapRect:zoomRect animated:YES];
[self.mapView setVisibleMapRect:zoomRect edgePadding:UIEdgeInsetsMake(10, 10, 10, 10) animated:YES];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment