Last active
October 26, 2018 16:59
-
-
Save minikin/d4aa2d2ff6968523b23e to your computer and use it in GitHub Desktop.
Zooms out a MKMapView to enclose all annotations (inc. current location).Swift.
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
| func zoomMapToFitAnnotations() { | |
| var zoomRect : MKMapRect = MKMapRectNull | |
| for annotation in mapView.annotations as! [MKAnnotation] { | |
| var annotationPoint = MKMapPointForCoordinate(annotation.coordinate) | |
| var pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0) | |
| if MKMapRectIsNull(zoomRect) { | |
| zoomRect = pointRect | |
| } else { | |
| zoomRect = MKMapRectUnion(zoomRect, pointRect) | |
| } | |
| } | |
| mapView.setVisibleMapRect(zoomRect, edgePadding:UIEdgeInsetsMake(10, 10, 10, 10), animated: false) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks perfectly !