Skip to content

Instantly share code, notes, and snippets.

@minikin
Last active October 26, 2018 16:59
Show Gist options
  • Select an option

  • Save minikin/d4aa2d2ff6968523b23e to your computer and use it in GitHub Desktop.

Select an option

Save minikin/d4aa2d2ff6968523b23e to your computer and use it in GitHub Desktop.
Zooms out a MKMapView to enclose all annotations (inc. current location).Swift.
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)
}
@danger2k7
Copy link

Thanks perfectly !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment