-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathContentView.swift
63 lines (51 loc) · 1.79 KB
/
ContentView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//
// ContentView.swift
// MapViewExample
//
// Created by Sören Gade on 21.02.20.
//
import SwiftUI
import SwiftUIMapView
import CoreLocation
import MapKit
struct ContentView: View {
let type: MKMapType = .standard
@State var region: MKCoordinateRegion? = MKCoordinateRegion(center: .applePark, span: MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05))
let trackingMode: MKUserTrackingMode = .none
let annotations: [MapViewAnnotation] = [ExampleAnnotation].examples
@State var selectedAnnotations: [MapViewAnnotation] = []
var body: some View {
VStack {
MapView(mapType: self.type,
region: self.$region,
userTrackingMode: self.trackingMode,
annotations: self.annotations,
selectedAnnotations: self.$selectedAnnotations)
.edgesIgnoringSafeArea(.all)
ForEach(self.selectedAnnotations.compactMap { $0 as? ExampleAnnotation }) { annotation in
Text("\( annotation.title ?? "" )")
}
if self.region != nil {
Text("\( self.regionToString(self.region!) )")
}
}
.onAppear {
// this is required to display the user's current location
self.requestLocationUsage()
}
}
func regionToString(_ region: MKCoordinateRegion) -> String {
"\(region.center.latitude), \(region.center.longitude)"
}
let locationManager = CLLocationManager()
private func requestLocationUsage() {
self.locationManager.requestWhenInUseAuthorization()
}
}
#if DEBUG
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
#endif