Mercurial > public > lazybear
comparison LazyBear/Helpers/MapSection.swift @ 465:6953d83060a4
New design
author | Dennis Concepción Martín <dennisconcepcionmartin@gmail.com> |
---|---|
date | Sat, 17 Jul 2021 17:58:57 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
464:04e430ef254a | 465:6953d83060a4 |
---|---|
1 // | |
2 // MapSection.swift | |
3 // lazybear | |
4 // | |
5 // Created by Dennis Concepción Martín on 17/07/2021. | |
6 // | |
7 | |
8 import SwiftUI | |
9 import MapKit | |
10 | |
11 struct MapSection: View { | |
12 var state: String | |
13 | |
14 var body: some View { | |
15 let states: [UsStatesModel] = parseJson("UsStatesCoordinates.json") | |
16 let stateObject = states.first(where: { $0.state == state }) | |
17 let region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: stateObject?.latitude ?? 40.741895, longitude: stateObject?.longitude ?? -73.989308), span: MKCoordinateSpan(latitudeDelta: 3.5, longitudeDelta: 3.5)) | |
18 | |
19 Map(coordinateRegion: .constant(region)) | |
20 .frame(height: 350) | |
21 } | |
22 | |
23 // MARK: - PARSE JSON | |
24 private func parseJson<T: Decodable>(_ filename: String) -> T { | |
25 let data: Data | |
26 | |
27 guard let file = Bundle.main.url(forResource: filename, withExtension: nil) | |
28 else { | |
29 fatalError("Couldn't find \(filename) in main bundle.") | |
30 } | |
31 | |
32 do { | |
33 data = try Data(contentsOf: file) | |
34 } catch { | |
35 fatalError("Couldn't load \(filename) from main bundle:\n\(error)") | |
36 } | |
37 | |
38 do { | |
39 let decoder = JSONDecoder() | |
40 return try decoder.decode(T.self, from: data) | |
41 } catch { | |
42 fatalError("Couldn't parse \(filename) as \(T.self):\n\(error)") | |
43 } | |
44 } | |
45 } | |
46 | |
47 struct MapSection_Previews: PreviewProvider { | |
48 static var previews: some View { | |
49 MapSection(state: "California") | |
50 } | |
51 } |