Last active
July 9, 2021 07:06
-
-
Save robertmryan/982e18b8b398690914180cd1ae28fce4 to your computer and use it in GitHub Desktop.
MKMapSnapshotter with MKMarkerAnnotationView - https://stackoverflow.com/q/58613580/1271826
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
func generateMap(with coordinates: [CLLocationCoordinate2D]) { | |
var annotationViews = [MKMarkerAnnotationView]() | |
let startCoord = coordinates.first! | |
let endCoord = coordinates.last! | |
annotationViews.append(createMapMarkerAnnotation(text: "A", coordinates: startCoord, color: .green)) | |
annotationViews.append(createMapMarkerAnnotation(text: "B", coordinates: endCoord, color: .green)) | |
let snapshotter = MKMapSnapshotter(options: options) | |
snapshotter.start { snapshot, error in | |
guard error == nil, let snapshot = snapshot else { return } | |
let image = snapshot.image | |
let finalImage = UIGraphicsImageRenderer(size: image.size).image { _ in | |
image.draw(at: .zero) | |
for view in annotationViews { | |
let point = snapshot.point(for: view.annotation!.coordinate) | |
view.drawHierarchy(in: CGRect(x: point.x - view.bounds.width / 2, y: point.y - view.bounds.height, width: view.bounds.width, height: view.bounds.height), afterScreenUpdates: true) | |
} | |
} | |
// do something with `finalImage` | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The above generated: