Skip to content

Instantly share code, notes, and snippets.

@robertmryan
Last active July 9, 2021 07:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robertmryan/982e18b8b398690914180cd1ae28fce4 to your computer and use it in GitHub Desktop.
Save robertmryan/982e18b8b398690914180cd1ae28fce4 to your computer and use it in GitHub Desktop.
MKMapSnapshotter with MKMarkerAnnotationView - https://stackoverflow.com/q/58613580/1271826
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`
}
}
@robertmryan
Copy link
Author

The above generated:

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment