Created
April 29, 2015 08:17
-
-
Save plumhead/c93bd759f986752228df to your computer and use it in GitHub Desktop.
MKCoordinateRegion extension to determine region needed to fit a set of locations with zoom factor
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 MKCoordinateRegion { | |
static func region(forLocations waypoints: [CLLocation], zoomPercent: Double = 0.2) -> MKCoordinateRegion { | |
var r = MKMapRectNull | |
for wp in waypoints { | |
var point = MKMapPointForCoordinate(wp.coordinate) | |
r = MKMapRectUnion(r, MKMapRectMake(point.x, point.y, 0, 0)) | |
} | |
let zoomed = MKMapRectMake(r.origin.x-r.size.width*zoomPercent, r.origin.y-r.size.height*zoomPercent, r.size.width*(1+zoomPercent*2), r.size.height*(1+zoomPercent*2)) | |
return MKCoordinateRegionForMapRect(zoomed) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment