-
Notifications
You must be signed in to change notification settings - Fork 325
Expand file tree
/
Copy pathCarPlayMapViewController.swift
More file actions
398 lines (312 loc) · 14.7 KB
/
Copy pathCarPlayMapViewController.swift
File metadata and controls
398 lines (312 loc) · 14.7 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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
import Foundation
@_spi(Restricted) import MapboxMaps
import MapboxCoreNavigation
import MapboxDirections
import os.log
#if canImport(CarPlay)
import CarPlay
/**
`CarPlayMapViewController` is responsible for administering the Mapbox map, the interface styles and the map template buttons to display on CarPlay.
*/
@available(iOS 12.0, *)
open class CarPlayMapViewController: UIViewController {
// MARK: UI Elements Configuration
/**
The view controller’s delegate.
*/
public weak var delegate: CarPlayMapViewControllerDelegate?
/**
Controls the styling of CarPlayMapViewController and its components.
The style can be modified programmatically by using `StyleManager.applyStyle(type:)`.
*/
public private(set) var styleManager: StyleManager?
/**
A very coarse location manager used for distinguishing between daytime and nighttime.
*/
fileprivate let coarseLocationManager: CLLocationManager = {
let coarseLocationManager = CLLocationManager()
coarseLocationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers
return coarseLocationManager
}()
/**
A view that displays the current speed limit.
*/
public var speedLimitView: SpeedLimitView!
/**
A view that displays the current road name.
*/
public var wayNameView: WayNameView!
/**
The interface styles available to `styleManager` for display.
*/
var styles: [Style] {
didSet {
styleManager?.styles = styles
}
}
var navigationMapView: NavigationMapView {
get {
return self.view as! NavigationMapView
}
}
// MARK: Bar Buttons Configuration
/**
The map button for recentering the map view if a user action causes it to stop following the user.
*/
public lazy var recenterButton: CPMapButton = {
let recenter = CPMapButton { [weak self] button in
self?.navigationMapView.navigationCamera.follow()
button.isHidden = true
}
let bundle = Bundle.mapboxNavigation
recenter.image = UIImage(named: "carplay_locate", in: bundle, compatibleWith: traitCollection)
return recenter
}()
/**
The map button for zooming in the current map view.
*/
public lazy var zoomInButton: CPMapButton = {
let zoomInButton = CPMapButton { [weak self] (button) in
guard let self = self,
let mapView = self.navigationMapView.mapView else { return }
self.navigationMapView.navigationCamera.stop()
var cameraOptions = CameraOptions(cameraState: mapView.cameraState)
cameraOptions.zoom = mapView.cameraState.zoom + 1.0
mapView.mapboxMap.setCamera(to: cameraOptions)
}
let bundle = Bundle.mapboxNavigation
zoomInButton.image = UIImage(named: "carplay_plus", in: bundle, compatibleWith: traitCollection)
return zoomInButton
}()
/**
The map button for zooming out the current map view.
*/
public lazy var zoomOutButton: CPMapButton = {
let zoomOutButton = CPMapButton { [weak self] button in
guard let self = self,
let mapView = self.navigationMapView.mapView else { return }
self.navigationMapView.navigationCamera.stop()
var cameraOptions = CameraOptions(cameraState: mapView.cameraState)
cameraOptions.zoom = mapView.cameraState.zoom - 1.0
mapView.mapboxMap.setCamera(to: cameraOptions)
}
let bundle = Bundle.mapboxNavigation
zoomOutButton.image = UIImage(named: "carplay_minus", in: bundle, compatibleWith: traitCollection)
return zoomOutButton
}()
/**
The map button property for hiding or showing the pan map button.
*/
public internal(set) var panMapButton: CPMapButton?
/**
The map button property for exiting the pan map mode.
*/
public internal(set) var dismissPanningButton: CPMapButton?
/**
Creates a new pan map button for the CarPlay map view controller.
- parameter mapTemplate: The map template available to the pan map button for display.
- returns: `CPMapButton` instance.
*/
@discardableResult public func panningInterfaceDisplayButton(for mapTemplate: CPMapTemplate) -> CPMapButton {
let panButton = CPMapButton { [weak mapTemplate] _ in
guard let mapTemplate = mapTemplate else { return }
if !mapTemplate.isPanningInterfaceVisible {
mapTemplate.showPanningInterface(animated: true)
}
}
let bundle = Bundle.mapboxNavigation
panButton.image = UIImage(named: "carplay_pan", in: bundle, compatibleWith: traitCollection)
return panButton
}
/**
Creates a new close button to dismiss the visible panning buttons on the map.
- parameter mapTemplate: The map template available to the pan map button for display.
- returns: `CPMapButton` instance.
*/
@discardableResult public func panningInterfaceDismissalButton(for mapTemplate: CPMapTemplate) -> CPMapButton {
let defaultButtons = mapTemplate.mapButtons
let closeButton = CPMapButton { [weak mapTemplate] _ in
guard let mapTemplate = mapTemplate else { return }
mapTemplate.mapButtons = defaultButtons
mapTemplate.dismissPanningInterface(animated: true)
}
let bundle = Bundle.mapboxNavigation
closeButton.image = UIImage(named: "carplay_close", in: bundle, compatibleWith: traitCollection)
return closeButton
}
private var safeTrailingSpeedLimitViewConstraint: NSLayoutConstraint!
private var trailingSpeedLimitViewConstraint: NSLayoutConstraint!
private let logger: OSLog = .init(subsystem: "com.mapbox.navigation", category: "CarPlayMapViewController")
// MARK: Initialization Methods
/**
Initializes a new CarPlay map view controller.
- parameter styles: The interface styles initially available to the style manager for display.
*/
public required init(styles: [Style]) {
self.styles = styles
super.init(nibName: nil, bundle: nil)
}
/**
Returns a `CarPlayMapViewController` object initialized from data in a given unarchiver.
- parameter coder: An unarchiver object.
*/
public required init?(coder decoder: NSCoder) {
guard let styles = decoder.decodeObject(of: [NSArray.self, Style.self], forKey: "styles") as? [Style] else {
return nil
}
self.styles = styles
super.init(coder: decoder)
}
public override func encode(with aCoder: NSCoder) {
super.encode(with: aCoder)
aCoder.encode(styles, forKey: "styles")
}
deinit {
unsubscribeFromFreeDriveNotifications()
}
func setupNavigationMapView() {
let navigationMapView = NavigationMapView(frame: UIScreen.main.bounds, navigationCameraType: .carPlay)
navigationMapView.delegate = self
navigationMapView.mapView.mapboxMap.onEvery(.styleLoaded) { [weak navigationMapView] _ in
navigationMapView?.localizeLabels()
}
navigationMapView.userLocationStyle = .puck2D()
navigationMapView.mapView.ornaments.options.logo.visibility = .hidden
navigationMapView.mapView.ornaments.options.attributionButton.visibility = .hidden
self.view = navigationMapView
}
func setupStyleManager() {
styleManager = StyleManager()
styleManager?.delegate = self
styleManager?.styles = styles
}
func setupSpeedLimitView() {
let speedLimitView = SpeedLimitView()
speedLimitView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(speedLimitView)
speedLimitView.topAnchor.constraint(equalTo: view.safeTopAnchor, constant: 8).isActive = true
safeTrailingSpeedLimitViewConstraint = speedLimitView.trailingAnchor.constraint(equalTo: view.safeTrailingAnchor,
constant: -8)
trailingSpeedLimitViewConstraint = speedLimitView.trailingAnchor.constraint(equalTo: view.trailingAnchor,
constant: -8)
speedLimitView.widthAnchor.constraint(equalToConstant: 30).isActive = true
speedLimitView.heightAnchor.constraint(equalToConstant: 30).isActive = true
self.speedLimitView = speedLimitView
}
func setupWayNameView() {
let wayNameView: WayNameView = .forAutoLayout()
wayNameView.containerView.isHidden = true
wayNameView.containerView.clipsToBounds = true
wayNameView.label.textAlignment = .center
view.addSubview(wayNameView)
NSLayoutConstraint.activate([
wayNameView.bottomAnchor.constraint(equalTo: view.safeBottomAnchor, constant: -8),
wayNameView.centerXAnchor.constraint(equalTo: view.safeCenterXAnchor),
wayNameView.widthAnchor.constraint(lessThanOrEqualTo: view.safeWidthAnchor, multiplier: 0.95)
])
self.wayNameView = wayNameView
}
func setupPassiveLocationProvider() {
let passiveLocationManager = PassiveLocationManager()
let passiveLocationProvider = PassiveLocationProvider(locationManager: passiveLocationManager)
navigationMapView.mapView.location.overrideLocationProvider(with: passiveLocationProvider)
subscribeForFreeDriveNotifications()
}
// MARK: Notifications Observer Methods
func subscribeForFreeDriveNotifications() {
NotificationCenter.default.addObserver(self,
selector: #selector(didUpdatePassiveLocation),
name: .passiveLocationManagerDidUpdate,
object: nil)
os_log("CarPlayMapViewController did subscribe to free drive notifications.", log: logger, type: .info)
}
func unsubscribeFromFreeDriveNotifications() {
NotificationCenter.default.removeObserver(self,
name: .passiveLocationManagerDidUpdate,
object: nil)
os_log("CarPlayMapViewController did unsubscribe from free drive notifications.", log: logger, type: .info)
}
@objc func didUpdatePassiveLocation(_ notification: Notification) {
speedLimitView.signStandard = notification.userInfo?[PassiveLocationManager.NotificationUserInfoKey.signStandardKey] as? SignStandard
speedLimitView.speedLimit = notification.userInfo?[PassiveLocationManager.NotificationUserInfoKey.speedLimitKey] as? Measurement<UnitSpeed>
if let roadName = notification.userInfo?[PassiveLocationManager.NotificationUserInfoKey.roadNameKey] as? String {
wayNameView.text = roadName.nonEmptyString
wayNameView.containerView.isHidden = roadName.isEmpty
} else {
wayNameView.containerView.isHidden = true
}
}
// MARK: UIViewController Lifecycle Methods
public override func loadView() {
setupNavigationMapView()
setupPassiveLocationProvider()
}
public override func viewDidLoad() {
super.viewDidLoad()
setupStyleManager()
setupSpeedLimitView()
setupWayNameView()
navigationMapView.navigationCamera.follow()
}
public override func viewSafeAreaInsetsDidChange() {
super.viewSafeAreaInsetsDidChange()
// Trigger update of view constraints to correctly position views like `SpeedLimitView`.
view.setNeedsUpdateConstraints()
guard let activeRoute = navigationMapView.routes?.first else {
navigationMapView.navigationCamera.follow()
return
}
if navigationMapView.navigationCamera.state == .idle {
var cameraOptions = CameraOptions(cameraState: navigationMapView.mapView.cameraState)
cameraOptions.pitch = 0
navigationMapView.mapView.mapboxMap.setCamera(to: cameraOptions)
navigationMapView.fitCamera(to: [activeRoute])
}
}
public override func updateViewConstraints() {
if view.safeAreaInsets.right > 38.0 {
safeTrailingSpeedLimitViewConstraint.isActive = true
trailingSpeedLimitViewConstraint.isActive = false
} else {
safeTrailingSpeedLimitViewConstraint.isActive = false
trailingSpeedLimitViewConstraint.isActive = true
}
super.updateViewConstraints()
}
}
// MARK: StyleManagerDelegate Methods
@available(iOS 12.0, *)
extension CarPlayMapViewController: StyleManagerDelegate {
public func location(for styleManager: StyleManager) -> CLLocation? {
var latestLocation: CLLocation? = nil
if let coordinate = navigationMapView.mapView.location.latestLocation?.coordinate {
latestLocation = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)
}
return navigationMapView.mostRecentUserCourseViewLocation ??
latestLocation ??
coarseLocationManager.location
}
public func styleManager(_ styleManager: StyleManager, didApply style: Style) {
let mapboxMapStyle = navigationMapView.mapView.mapboxMap.style
if mapboxMapStyle.uri?.rawValue != style.mapStyleURL.absoluteString {
mapboxMapStyle.uri = StyleURI(url: style.previewMapStyleURL)
}
}
public func styleManagerDidRefreshAppearance(_ styleManager: StyleManager) {
guard let mapboxMap = navigationMapView.mapView.mapboxMap,
let styleURI = mapboxMap.style.uri else { return }
mapboxMap.loadStyleURI(styleURI)
}
}
// MARK: NavigationMapViewDelegate Methods
@available(iOS 12.0, *)
extension CarPlayMapViewController: NavigationMapViewDelegate {
public func navigationMapView(_ navigationMapView: NavigationMapView,
didAdd finalDestinationAnnotation: PointAnnotation,
pointAnnotationManager: PointAnnotationManager) {
delegate?.carPlayMapViewController(self,
didAdd: finalDestinationAnnotation,
pointAnnotationManager: pointAnnotationManager)
}
}
#endif