Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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
You can’t perform that action at this time.