-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathMapViewDelegate.swift
208 lines (178 loc) · 9.55 KB
/
MapViewDelegate.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
//
// Localized by nitricware on 19/08/19.
//
import MapKit
import CoreGPX
/// Handles all delegate functions of the GPX Mapview
///
class MapViewDelegate: NSObject, MKMapViewDelegate, UIAlertViewDelegate {
/// The Waypoint is being edited (if there is any)
var waypointBeingEdited: GPXWaypoint = GPXWaypoint()
/// Displays a pin with whose annotation (bubble) will include delete and edit buttons.
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation.isKind(of: MKUserLocation.self) {
return nil
}
let annotationView: MKPinAnnotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "PinView")
annotationView.canShowCallout = true
annotationView.isDraggable = true
let deleteButton: UIButton = UIButton(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
deleteButton.setImage(UIImage(named: "delete"), for: UIControl.State())
deleteButton.setImage(UIImage(named: "deleteHigh"), for: .highlighted)
deleteButton.tag = kDeleteWaypointAccesoryButtonTag
annotationView.rightCalloutAccessoryView = deleteButton
let editButton: UIButton = UIButton(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
editButton.setImage(UIImage(named: "edit"), for: UIControl.State())
editButton.setImage(UIImage(named: "editHigh"), for: .highlighted)
editButton.tag = kEditWaypointAccesoryButtonTag
annotationView.leftCalloutAccessoryView = editButton
return annotationView
}
/// Displays the line for each segment
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
if overlay.isKind(of: MKTileOverlay.self) {
return mapView.mapCacheRenderer(forOverlay: overlay)
}
if overlay is MKPolyline {
let pr = MKPolylineRenderer(overlay: overlay)
pr.alpha = 0.8
pr.strokeColor = UIColor.blue
if #available(iOS 13, *) {
if #available(iOS 14, *) {
pr.shouldRasterize = false
} else {
pr.shouldRasterize = true
}
if mapView.traitCollection.userInterfaceStyle == .dark {
pr.alpha = 0.5
pr.strokeColor = UIColor.yellow
}
}
pr.lineWidth = 3
return pr
}
return MKOverlayRenderer()
}
/// Handles the actions of delete and edit button
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
print("calloutAccesoryControlTapped ")
guard let waypoint = view.annotation as? GPXWaypoint else {
return
}
guard let button = control as? UIButton else {
return
}
guard let map = mapView as? GPXMapView else {
return
}
switch button.tag {
case kDeleteWaypointAccesoryButtonTag:
print("[calloutAccesoryControlTapped: DELETE button] deleting waypoint with name \(waypoint.name ?? "''")")
map.removeWaypoint(waypoint)
case kEditWaypointAccesoryButtonTag:
print("[calloutAccesoryControlTapped: EDIT] editing waypoint with name \(waypoint.name ?? "''")")
let indexofEditedWaypoint = map.session.waypoints.firstIndex(of: waypoint)
let alertController = UIAlertController(title: NSLocalizedString("EDIT_WAYPOINT_NAME_TITLE", comment: "no comment"),
message: NSLocalizedString("EDIT_WAYPOINT_NAME_MESSAGE", comment: "no comment"),
preferredStyle: .alert)
alertController.addTextField { (textField) in
textField.text = waypoint.title
textField.clearButtonMode = .always
}
let saveAction = UIAlertAction(title: NSLocalizedString("SAVE", comment: "no comment"), style: .default) { _ in
print("Edit waypoint alert view")
self.waypointBeingEdited.title = alertController.textFields?[0].text
map.coreDataHelper.update(toCoreData: self.waypointBeingEdited, from: indexofEditedWaypoint!)
}
let cancelAction = UIAlertAction(title: NSLocalizedString("CANCEL", comment: "no comment"), style: .cancel) { _ in }
alertController.addAction(saveAction)
alertController.addAction(cancelAction)
UIApplication.shared.keyWindow?.rootViewController?.present(alertController, animated: true)
self.waypointBeingEdited = waypoint
default:
print("[calloutAccesoryControlTapped ERROR] unknown control")
}
}
/// Handles the change of the coordinates when a pin is dropped.
func mapView(_ mapView: MKMapView,
annotationView view: MKAnnotationView,
didChange newState: MKAnnotationView.DragState,
fromOldState oldState: MKAnnotationView.DragState) {
// swiftlint:disable:next force_cast
let gpxMapView = mapView as! GPXMapView
if newState == MKAnnotationView.DragState.ending {
if let point = view.annotation as? GPXWaypoint {
point.elevation = nil
if let index = gpxMapView.session.waypoints.firstIndex(of: point) {
gpxMapView.coreDataHelper.update(toCoreData: point, from: index)
}
let titleDesc = String(describing: point.title)
let latDesc = String(describing: point.latitude)
let lonDesc = String(describing: point.longitude)
print("Annotation name: \(titleDesc) lat:\(latDesc) lon \(lonDesc)")
}
}
}
/// Adds the pin to the map with an animation (comes from the top of the screen)
func mapView(_ mapView: MKMapView, didAdd views: [MKAnnotationView]) {
var i = 0
// swiftlint:disable:next force_cast
let gpxMapView = mapView as! GPXMapView
var hasImpacted = false
// Adds the pins with an animation
for object in views {
i += 1
let annotationView = object as MKAnnotationView
// The only exception is the user location, we add to this the heading icon.
if annotationView.annotation!.isKind(of: MKUserLocation.self) {
if gpxMapView.headingImageView == nil {
let image = UIImage(named: "heading")!
gpxMapView.headingImageView = UIImageView(image: image)
gpxMapView.headingImageView!.frame = CGRect(x: (annotationView.frame.size.width - image.size.width)/2,
y: (annotationView.frame.size.height - image.size.height)/2,
width: image.size.width,
height: image.size.height)
annotationView.insertSubview(gpxMapView.headingImageView!, at: 0)
gpxMapView.headingImageView!.isHidden = true
}
continue
}
let point: MKMapPoint = MKMapPoint.init(annotationView.annotation!.coordinate)
if !mapView.visibleMapRect.contains(point) { continue }
let endFrame: CGRect = annotationView.frame
annotationView.frame = CGRect(x: annotationView.frame.origin.x, y: annotationView.frame.origin.y - mapView.superview!.frame.size.height,
width: annotationView.frame.size.width, height: annotationView.frame.size.height)
let interval: TimeInterval = 0.04 * 1.1
UIView.animate(withDuration: 0.5, delay: interval, options: UIView.AnimationOptions.curveLinear, animations: { () -> Void in
annotationView.frame = endFrame
}, completion: { (finished) -> Void in
if finished {
UIView.animate(withDuration: 0.05, animations: { () -> Void in
annotationView.transform = CGAffineTransform(a: 1.0, b: 0, c: 0, d: 0.8, tx: 0, ty: annotationView.frame.size.height*0.1)
}, completion: { _ -> Void in
UIView.animate(withDuration: 0.1, animations: { () -> Void in
annotationView.transform = CGAffineTransform.identity
})
if #available(iOS 10.0, *), !hasImpacted {
hasImpacted = true
UIImpactFeedbackGenerator(style: i > 2 ? .heavy : .medium).impactOccurred()
}
})
}
})
}
}
///
/// Adds a small arrow image to the annotationView.
/// This annotationView should be the MKUserLocation
///
func addHeadingView(toAnnotationView annotationView: MKAnnotationView) {
}
/// Updates map heading after user interactions end.
func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
guard let map = mapView as? GPXMapView else {
return
}
map.updateHeading()
}
}