-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContentView.swift
More file actions
155 lines (146 loc) · 6.02 KB
/
ContentView.swift
File metadata and controls
155 lines (146 loc) · 6.02 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import SwiftUI
import MapKit
struct TappableMapView: UIViewRepresentable {
let onTap: ((CLLocationCoordinate2D) -> Void)?
@Binding var annotations: [MKPointAnnotation]
@Binding var region: MKCoordinateRegion
@Binding var shouldCenter: Bool
func makeUIView(context: Context) -> MKMapView {
let mapView = MKMapView(frame: .zero)
mapView.delegate = context.coordinator
let tapGesture = UITapGestureRecognizer(target: context.coordinator,
action: #selector(Coordinator.handleTap(_:)))
mapView.addGestureRecognizer(tapGesture)
mapView.setRegion(region, animated: false)
return mapView
}
func updateUIView(_ uiView: MKMapView, context: Context) {
if shouldCenter {
uiView.setRegion(region, animated: true)
DispatchQueue.main.async {
self.shouldCenter = false
}
}
uiView.removeAnnotations(uiView.annotations)
uiView.addAnnotations(annotations)
}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
class Coordinator: NSObject, MKMapViewDelegate {
var parent: TappableMapView
init(_ parent: TappableMapView) {
self.parent = parent
}
@objc func handleTap(_ gestureRecognizer: UITapGestureRecognizer) {
guard let mapView = gestureRecognizer.view as? MKMapView else { return }
let tapPoint = gestureRecognizer.location(in: mapView)
let coordinate = mapView.convert(tapPoint, toCoordinateFrom: mapView)
parent.onTap?(coordinate)
}
func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
parent.region = mapView.region
}
}
}
struct ContentView: View {
@State private var annotations: [MKPointAnnotation] = {
let memorialAnnotation = MKPointAnnotation()
memorialAnnotation.coordinate = CLLocationCoordinate2D(latitude: 38.54237287621318,
longitude: -121.74955185519764)
memorialAnnotation.title = "Memorial Union"
return [memorialAnnotation]
}()
@State private var region = MKCoordinateRegion(
center: CLLocationCoordinate2D(latitude: 38.54237287621318,
longitude: -121.74955185519764),
latitudinalMeters: 5000,
longitudinalMeters: 5000
)
@State private var shouldCenter: Bool = false
@State private var tappedCoordinate: CLLocationCoordinate2D? = nil
@State private var newMarkerName: String = ""
@State private var showMarkerEditor: Bool = false
var body: some View {
ZStack {
TappableMapView(onTap: { coordinate in
tappedCoordinate = coordinate
newMarkerName = ""
showMarkerEditor = true
},
annotations: $annotations,
region: $region,
shouldCenter: $shouldCenter)
.edgesIgnoringSafeArea(.all)
VStack {
Spacer()
HStack {
Spacer()
Button {
region = MKCoordinateRegion(
center: CLLocationCoordinate2D(latitude: 38.54237287621318,
longitude: -121.74955185519764),
latitudinalMeters: 200,
longitudinalMeters: 200
)
shouldCenter = true
} label: {
Text("MU")
.padding()
}
Spacer()
}
.background(.thinMaterial)
}
if showMarkerEditor {
Color.black.opacity(0.4)
.edgesIgnoringSafeArea(.all)
VStack(spacing: 20) {
Text("Enter a name for the location:")
.font(.headline)
TextField("Location name", text: $newMarkerName)
.textFieldStyle(RoundedBorderTextFieldStyle())
.padding(.horizontal)
HStack {
Button(action: {
showMarkerEditor = false
}, label: {
Text("Cancel")
.frame(maxWidth: .infinity)
.padding()
.background(Color.gray.opacity(0.2))
.cornerRadius(8)
})
Button(action: {
if let coordinate = tappedCoordinate,
!newMarkerName.trimmingCharacters(in: .whitespaces).isEmpty {
let newAnnotation = MKPointAnnotation()
newAnnotation.coordinate = coordinate
newAnnotation.title = newMarkerName
annotations.append(newAnnotation)
}
showMarkerEditor = false
}, label: {
Text("Add")
.frame(maxWidth: .infinity)
.padding()
.background(Color.blue.opacity(0.7))
.foregroundColor(.white)
.cornerRadius(8)
})
}
.padding(.horizontal)
}
.padding()
.background(Color.white)
.cornerRadius(12)
.padding(40)
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}