Skip to content

Instantly share code, notes, and snippets.

@teawithfruit
Last active May 10, 2019 10:01
Show Gist options
  • Save teawithfruit/f95a722884ed9e74cad32a65b034f6a3 to your computer and use it in GitHub Desktop.
Save teawithfruit/f95a722884ed9e74cad32a65b034f6a3 to your computer and use it in GitHub Desktop.
Extension to fit annotaion with a given location
extension MKMapView {
func fitMapViewToAnnotaion(withEdgePadding edgePadding:UIEdgeInsets = UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20), andLocation givenLocation:CLLocationCoordinate2D?) -> Void {
var zoomRect:MKMapRect = MKMapRect.null
for annotation in annotations {
let aPoint:MKMapPoint = MKMapPoint(annotation.coordinate)
let rect:MKMapRect = MKMapRect(x: aPoint.x, y: aPoint.y, width: 0.1, height: 0.1)
if zoomRect.isNull {
zoomRect = rect
} else {
zoomRect = zoomRect.union(rect)
}
}
if let location = givenLocation {
let aPoint:MKMapPoint = MKMapPoint(location)
let rect:MKMapRect = MKMapRect(x: aPoint.x, y: aPoint.y, width: 0.1, height: 0.1)
if zoomRect.isNull {
zoomRect = rect
} else {
zoomRect = zoomRect.union(rect)
}
}
setVisibleMapRect(zoomRect, edgePadding: edgePadding, animated: true)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment