Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func generateImage(completion: @escaping (UIImage?) -> Void) {
- let mapSnapshotOptions = MKMapSnapshotter.Options()
- let span = MKCoordinateSpan(latitudeDelta: 0.008, longitudeDelta: 0.006)
- let region = MKCoordinateRegion(center: coordinates, span: span)
- // Set the region of the map that is rendered.
- mapSnapshotOptions.region = region
- // Set the scale of the image. We'll just use the scale of the current device, which is 2x scale on Retina screens.
- mapSnapshotOptions.scale = UIScreen.main.scale
- // Set the size of the image output.
- mapSnapshotOptions.size = CGSize(width: 300, height: 300)
- // Show buildings and Points of Interest on the snapshot
- mapSnapshotOptions.showsBuildings = true
- mapSnapshotOptions.showsPointsOfInterest = true
- let snapShotter = MKMapSnapshotter(options: mapSnapshotOptions)
- let rect = CGRect(x: 0, y: 0, width: 300, height: 300)
- snapShotter.start { (snapshot, error) in
- guard let snapshot = snapshot, error == nil else {
- completion(nil)
- return
- }
- UIGraphicsBeginImageContextWithOptions(mapSnapshotOptions.size, true, 0)
- snapshot.image.draw(at: .zero)
- let pinView = MKPinAnnotationView(annotation: nil, reuseIdentifier: nil)
- let pinImage = pinView.image
- var point = snapshot.point(for: self.coordinates)
- if rect.contains(point) {
- let pinCenterOffset = pinView.centerOffset
- point.x -= pinView.bounds.size.width / 2
- point.y -= pinView.bounds.size.height / 2
- point.x += pinCenterOffset.x
- point.y += pinCenterOffset.y
- pinImage?.draw(at: point)
- }
- let image = UIGraphicsGetImageFromCurrentImageContext()
- UIGraphicsEndImageContext()
- completion(image)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement