Mercurial > public > geoquiz
comparison GeoQuiz/Components/CityMapHelper.swift @ 19:f140bb277c96
refactor code
author | Dennis C. M. <dennis@denniscm.com> |
---|---|
date | Sun, 23 Oct 2022 00:11:38 +0100 |
parents | a793f33f05fb |
children |
comparison
equal
deleted
inserted
replaced
18:d20cf93c9812 | 19:f140bb277c96 |
---|---|
7 | 7 |
8 import SwiftUI | 8 import SwiftUI |
9 import MapKit | 9 import MapKit |
10 | 10 |
11 struct CityMap: View { | 11 struct CityMap: View { |
12 @ObservedObject var game: CityGame | 12 @ObservedObject var game: CityGameController |
13 @State private var mapImage: UIImage? = nil | 13 |
14 @StateObject var mapController: MapController | |
15 | |
16 init(game: CityGameController) { | |
17 self.game = game | |
18 self._mapController = StateObject(wrappedValue: MapController()) | |
19 } | |
14 | 20 |
15 var body: some View { | 21 var body: some View { |
16 VStack { | 22 VStack { |
17 if let mapImage = mapImage { | 23 if let mapImage = mapController.image { |
18 Image(uiImage: mapImage) | 24 Image(uiImage: mapImage) |
19 .resizable() | 25 .resizable() |
20 .scaledToFit() | 26 .scaledToFit() |
21 .clipShape(Circle()) | 27 .clipShape(Circle()) |
22 .overlay { | 28 .overlay { |
26 .shadow(radius: 10) | 32 .shadow(radius: 10) |
27 } else { | 33 } else { |
28 ProgressView() | 34 ProgressView() |
29 } | 35 } |
30 } | 36 } |
31 .onChange(of: game.correctAnswer.value) { _ in getMapImage() } | 37 .onChange(of: game.correctAnswer.value) { _ in |
32 .onAppear(perform: getMapImage) | 38 mapController.getMapImage(lat: game.correctAnswer.value.lat, lon: game.correctAnswer.value.lon) |
33 } | 39 } |
34 | 40 |
35 private func getMapImage() { | 41 .onAppear { |
36 let region = MKCoordinateRegion( | 42 mapController.getMapImage(lat: game.correctAnswer.value.lat, lon: game.correctAnswer.value.lon) |
37 center: CLLocationCoordinate2D( | |
38 latitude: game.correctAnswer.value.lat, | |
39 longitude: game.correctAnswer.value.lon | |
40 ), | |
41 span: MKCoordinateSpan( | |
42 latitudeDelta: 0.1, | |
43 longitudeDelta: 0.1 | |
44 ) | |
45 ) | |
46 | |
47 // Map options | |
48 let mapOptions = MKMapSnapshotter.Options() | |
49 mapOptions.region = region | |
50 mapOptions.size = CGSize(width: 500, height: 500) | |
51 mapOptions.pointOfInterestFilter = .excludingAll | |
52 | |
53 // Create the snapshotter and run it | |
54 let snapshotter = MKMapSnapshotter(options: mapOptions) | |
55 snapshotter.start { (snapshot, error) in | |
56 if let snapshot = snapshot { | |
57 self.mapImage = snapshot.image | |
58 } else if let error = error { | |
59 print(error.localizedDescription) | |
60 } | |
61 } | 43 } |
62 } | 44 } |
63 } | 45 } |
64 | 46 |
65 struct CityMap_Previews: PreviewProvider { | 47 struct CityMap_Previews: PreviewProvider { |
66 static var previews: some View { | 48 static var previews: some View { |
67 CityMap(game: CityGame()) | 49 CityMap(game: CityGameController()) |
68 } | 50 } |
69 } | 51 } |