Created
February 12, 2017 12:55
-
-
Save freak4pc/1748322a1b8bba23b6de3f47cb00c3ea to your computer and use it in GitHub Desktop.
Display list of coordinates / annotation correctly inside a map
This file contains hidden or 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
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