comparison GeoQuiz/Components/CityMapHelper.swift @ 10:a793f33f05fb

refactor code and fix layout
author Dennis C. M. <dennis@denniscm.com>
date Sat, 08 Oct 2022 21:36:40 +0200
parents GeoQuiz/Helpers/CityMap.swift@e09959b4e4a8
children f140bb277c96
comparison
equal deleted inserted replaced
9:3540c7efc216 10:a793f33f05fb
1 //
2 // CityMapHelper.swift
3 // GeoQuiz
4 //
5 // Created by Dennis Concepción Martín on 4/10/22.
6 //
7
8 import SwiftUI
9 import MapKit
10
11 struct CityMap: View {
12 @ObservedObject var game: CityGame
13 @State private var mapImage: UIImage? = nil
14
15 var body: some View {
16 VStack {
17 if let mapImage = mapImage {
18 Image(uiImage: mapImage)
19 .resizable()
20 .scaledToFit()
21 .clipShape(Circle())
22 .overlay {
23 Circle()
24 .strokeBorder(.white, lineWidth: 4)
25 }
26 .shadow(radius: 10)
27 } else {
28 ProgressView()
29 }
30 }
31 .onChange(of: game.correctAnswer.value) { _ in getMapImage() }
32 .onAppear(perform: getMapImage)
33 }
34
35 private func getMapImage() {
36 let region = MKCoordinateRegion(
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 }
62 }
63 }
64
65 struct CityMap_Previews: PreviewProvider {
66 static var previews: some View {
67 CityMap(game: CityGame())
68 }
69 }