Mercurial > public > geoquiz
comparison GeoQuiz/Helpers/CityMap.swift @ 8:e09959b4e4a8
fix bugs
author | Dennis C. M. <dennis@denniscm.com> |
---|---|
date | Thu, 06 Oct 2022 11:14:34 +0200 |
parents | d945e52b0704 |
children |
comparison
equal
deleted
inserted
replaced
7:d945e52b0704 | 8:e09959b4e4a8 |
---|---|
4 // | 4 // |
5 // Created by Dennis Concepción Martín on 4/10/22. | 5 // Created by Dennis Concepción Martín on 4/10/22. |
6 // | 6 // |
7 | 7 |
8 import SwiftUI | 8 import SwiftUI |
9 import MapKit | |
9 | 10 |
10 struct CityMap: View { | 11 struct CityMap: View { |
11 @ObservedObject var game: CityGame | 12 @ObservedObject var game: CityGame |
13 @State private var mapImage: UIImage? = nil | |
14 | |
15 let geo: GeometryProxy | |
12 | 16 |
13 var body: some View { | 17 var body: some View { |
14 Group { | 18 VStack { |
15 if let mapImage = game.mapImage { | 19 if let mapImage = mapImage { |
16 Image(uiImage: mapImage) | 20 Image(uiImage: mapImage) |
17 .resizable() | 21 .resizable() |
18 .scaledToFit() | 22 .scaledToFit() |
19 .clipShape(RoundedRectangle(cornerRadius: 20)) | 23 .clipShape(RoundedRectangle(cornerRadius: 20)) |
20 .shadow(radius: 10) | 24 .shadow(radius: 10) |
21 } else { | 25 } else { |
22 ProgressView() | 26 ProgressView() |
23 } | 27 } |
24 } | 28 } |
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 } | |
25 } | 60 } |
26 } | 61 } |
27 | 62 |
28 struct CityMap_Previews: PreviewProvider { | 63 //struct CityMap_Previews: PreviewProvider { |
29 static var previews: some View { | 64 // static var previews: some View { |
30 CityMap(game: CityGame()) | 65 // CityMap(game: CityGame()) |
31 } | 66 // } |
32 } | 67 //} |