6
|
1 //
|
|
2 // CityMap.swift
|
|
3 // GeoQuiz
|
|
4 //
|
7
|
5 // Created by Dennis Concepción Martín on 4/10/22.
|
6
|
6 //
|
|
7
|
|
8 import SwiftUI
|
8
|
9 import MapKit
|
6
|
10
|
|
11 struct CityMap: View {
|
7
|
12 @ObservedObject var game: CityGame
|
8
|
13 @State private var mapImage: UIImage? = nil
|
|
14
|
|
15 let geo: GeometryProxy
|
6
|
16
|
|
17 var body: some View {
|
8
|
18 VStack {
|
|
19 if let mapImage = mapImage {
|
7
|
20 Image(uiImage: mapImage)
|
|
21 .resizable()
|
|
22 .scaledToFit()
|
|
23 .clipShape(RoundedRectangle(cornerRadius: 20))
|
|
24 .shadow(radius: 10)
|
|
25 } else {
|
|
26 ProgressView()
|
|
27 }
|
|
28 }
|
8
|
29 .onChange(of: game.correctAnswer.value) { _ in getMapImage() }
|
|
30 .onAppear(perform: getMapImage)
|
|
31 }
|
|
32
|
|
33 private func getMapImage() {
|
|
34 let region = MKCoordinateRegion(
|
|
35 center: CLLocationCoordinate2D(
|
|
36 latitude: game.correctAnswer.value.lat,
|
|
37 longitude: game.correctAnswer.value.lon
|
|
38 ),
|
|
39 span: MKCoordinateSpan(
|
|
40 latitudeDelta: 0.1,
|
|
41 longitudeDelta: 0.1
|
|
42 )
|
|
43 )
|
|
44
|
|
45 // Map options
|
|
46 let mapOptions = MKMapSnapshotter.Options()
|
|
47 mapOptions.region = region
|
|
48 mapOptions.size = CGSize(width: geo.size.width * 0.8, height: geo.size.width * 0.8)
|
|
49 mapOptions.pointOfInterestFilter = .excludingAll
|
|
50
|
|
51 // Create the snapshotter and run it
|
|
52 let snapshotter = MKMapSnapshotter(options: mapOptions)
|
|
53 snapshotter.start { (snapshot, error) in
|
|
54 if let snapshot = snapshot {
|
|
55 self.mapImage = snapshot.image
|
|
56 } else if let error = error {
|
|
57 print(error.localizedDescription)
|
|
58 }
|
|
59 }
|
6
|
60 }
|
|
61 }
|
|
62
|
8
|
63 //struct CityMap_Previews: PreviewProvider {
|
|
64 // static var previews: some View {
|
|
65 // CityMap(game: CityGame())
|
|
66 // }
|
|
67 //}
|