Last active
May 10, 2019 10:01
-
-
Save teawithfruit/f95a722884ed9e74cad32a65b034f6a3 to your computer and use it in GitHub Desktop.
Extension to fit annotaion with a given location
This file contains 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
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