Skip to content

Instantly share code, notes, and snippets.

@freak4pc
Created February 12, 2017 12:55
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save freak4pc/1748322a1b8bba23b6de3f47cb00c3ea to your computer and use it in GitHub Desktop.
Save freak4pc/1748322a1b8bba23b6de3f47cb00c3ea to your computer and use it in GitHub Desktop.
Display list of coordinates / annotation correctly inside a map
public extension MKMapView {
public static func visibleRect(for coords: [CLLocationCoordinate2D]) -> MKMapRect {
return coords.reduce(MKMapRectNull) { outRect, coord in
let point = MKMapPointForCoordinate(coord)
let rect = MKMapRectMake(point.x, point.y, 0.1, 0.1)
let union = MKMapRectUnion(rect, outRect)
return union
}
}
public func fitCoordinates(_ coords: [CLLocationCoordinate2D],
animated: Bool = true,
insets: UIEdgeInsets = UIEdgeInsets(top: 25, left: 25, bottom: 25, right: 25)) {
let rect = MKMapView.visibleRect(for: coords)
self.setVisibleMapRect(rect, edgePadding: insets, animated: animated)
}
}
// Usage: map.fitCoordinates([coord1, coord2, coord3])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment