19
|
1 //
|
|
2 // MapController.swift
|
|
3 // GeoQuiz
|
|
4 //
|
|
5 // Created by Dennis Concepción Martín on 22/10/22.
|
|
6 //
|
|
7
|
|
8 import Foundation
|
|
9 import MapKit
|
|
10
|
|
11 class MapController: ObservableObject {
|
|
12 @Published var image: UIImage? = nil
|
|
13
|
|
14 func getMapImage(lat: Double, lon: Double) {
|
|
15 let region = MKCoordinateRegion(
|
|
16 center: CLLocationCoordinate2D(latitude: lat, longitude: lon),
|
|
17 span: MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1)
|
|
18 )
|
|
19
|
|
20 // Map options
|
|
21 let mapOptions = MKMapSnapshotter.Options()
|
|
22 mapOptions.region = region
|
|
23 mapOptions.size = CGSize(width: 500, height: 500)
|
|
24 mapOptions.pointOfInterestFilter = .excludingAll
|
|
25
|
|
26 // Create the snapshotter and run it
|
|
27 let snapshotter = MKMapSnapshotter(options: mapOptions)
|
|
28 snapshotter.start { (snapshot, error) in
|
|
29 if let snapshot = snapshot {
|
|
30 self.image = snapshot.image
|
|
31 } else if let error = error {
|
|
32 print(error.localizedDescription)
|
|
33 }
|
|
34 }
|
|
35 }
|
|
36 }
|