diff GeoQuiz/Models/Controllers/MapController.swift @ 26:425078c01194

refactor code
author Dennis C. M. <dennis@denniscm.com>
date Wed, 09 Nov 2022 10:30:01 +0100
parents GeoQuiz/Logic/MapController.swift@f140bb277c96
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/GeoQuiz/Models/Controllers/MapController.swift	Wed Nov 09 10:30:01 2022 +0100
@@ -0,0 +1,36 @@
+//
+//  MapController.swift
+//  GeoQuiz
+//
+//  Created by Dennis Concepción Martín on 22/10/22.
+//
+
+import Foundation
+import MapKit
+
+class MapController: ObservableObject {
+    @Published var image: UIImage? = nil
+    
+    func getMapImage(lat: Double, lon: Double) {
+        let region = MKCoordinateRegion(
+            center: CLLocationCoordinate2D(latitude: lat, longitude: lon),
+            span: MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05)
+        )
+
+        // Map options
+        let mapOptions = MKMapSnapshotter.Options()
+        mapOptions.region = region
+        mapOptions.size = CGSize(width: 500, height: 500)
+        mapOptions.pointOfInterestFilter = .excludingAll
+
+        // Create the snapshotter and run it
+        let snapshotter = MKMapSnapshotter(options: mapOptions)
+        snapshotter.start { (snapshot, error) in
+            if let snapshot = snapshot {
+                self.image = snapshot.image
+            } else if let error = error {
+                print(error.localizedDescription)
+            }
+        }
+    }
+}