Skip to content

Instantly share code, notes, and snippets.

@plumhead
Created April 29, 2015 08:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plumhead/c93bd759f986752228df to your computer and use it in GitHub Desktop.
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
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