-
Notifications
You must be signed in to change notification settings - Fork 325
Expand file tree
/
Copy pathViewController+FreeDrive.swift
More file actions
169 lines (140 loc) · 8.16 KB
/
Copy pathViewController+FreeDrive.swift
File metadata and controls
169 lines (140 loc) · 8.16 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
import UIKit
import Turf
import MapboxDirections
import MapboxCoreNavigation
import MapboxNavigation
import MapboxCoreMaps
import MapboxMaps
// MARK: - Free-driving methods
extension ViewController {
func setupPassiveLocationProvider() {
setupFreeDriveStyledFeatures()
let passiveLocationManager = PassiveLocationManager()
self.passiveLocationManager = passiveLocationManager
// Provide the ElectronicHorizonOptions for PassiveLocationManager to start electronic horizon updates.
let options = ElectronicHorizonOptions(length: 100, expansionLevel: 1, branchLength: 20, minTimeDeltaBetweenUpdates: nil)
passiveLocationManager.startUpdatingElectronicHorizon(with: options)
let passiveLocationProvider = PassiveLocationProvider(locationManager: passiveLocationManager)
navigationMapView.mapView.location.overrideLocationProvider(with: passiveLocationProvider)
subscribeForFreeDriveNotifications()
}
func subscribeForFreeDriveNotifications() {
NotificationCenter.default.addObserver(self,
selector: #selector(didUpdatePassiveLocation),
name: .passiveLocationManagerDidUpdate,
object: nil)
NotificationCenter.default.addObserver(self,
selector: #selector(didUpdateElectronicHorizonPosition),
name: .electronicHorizonDidUpdatePosition,
object: nil)
}
func unsubscribeFromFreeDriveNotifications() {
NotificationCenter.default.removeObserver(self, name: .passiveLocationManagerDidUpdate, object: nil)
NotificationCenter.default.removeObserver(self, name: .electronicHorizonDidUpdatePosition, object: nil)
}
@objc func didUpdatePassiveLocation(_ notification: Notification) {
if let roadName = notification.userInfo?[PassiveLocationManager.NotificationUserInfoKey.roadNameKey] as? String {
title = roadName
}
if let location = notification.userInfo?[PassiveLocationManager.NotificationUserInfoKey.locationKey] as? CLLocation {
speedLimitView.currentSpeed = location.speed
trackStyledFeature.lineString.coordinates.append(contentsOf: [location.coordinate])
// Update user puck to the most recent location.
navigationMapView.moveUserLocation(to: location, animated: true)
}
if let rawLocation = notification.userInfo?[PassiveLocationManager.NotificationUserInfoKey.rawLocationKey] as? CLLocation {
rawTrackStyledFeature.lineString.coordinates.append(contentsOf: [rawLocation.coordinate])
}
speedLimitView.signStandard = notification.userInfo?[PassiveLocationManager.NotificationUserInfoKey.signStandardKey] as? SignStandard
speedLimitView.speedLimit = notification.userInfo?[PassiveLocationManager.NotificationUserInfoKey.speedLimitKey] as? Measurement<UnitSpeed>
updateFreeDriveStyledFeatures()
}
func setupFreeDriveStyledFeatures() {
trackStyledFeature = StyledFeature(sourceIdentifier: "trackSourceIdentifier",
layerIdentifier: "trackLayerIdentifier",
color: .darkGray,
lineWidth: 3.0,
lineString: LineString([]))
rawTrackStyledFeature = StyledFeature(sourceIdentifier: "rawTrackSourceIdentifier",
layerIdentifier: "rawTrackLayerIdentifier",
color: .lightGray,
lineWidth: 3.0,
lineString: LineString([]))
navigationMapView.mapView.mapboxMap.onNext(event: .styleLoaded, handler: { [weak self] _ in
guard let self = self else { return }
self.addStyledFeature(self.trackStyledFeature)
self.addStyledFeature(self.rawTrackStyledFeature)
})
}
func updateFreeDriveStyledFeatures() {
do {
let style = navigationMapView.mapView.mapboxMap.style
if style.sourceExists(withId: trackStyledFeature.sourceIdentifier) {
let feature = Feature(geometry: .lineString(trackStyledFeature.lineString))
try style.updateGeoJSONSource(withId: trackStyledFeature.sourceIdentifier,
geoJSON: .feature(feature))
}
if style.sourceExists(withId: rawTrackStyledFeature.sourceIdentifier) {
let feature = Feature(geometry: .lineString(rawTrackStyledFeature.lineString))
try style.updateGeoJSONSource(withId: rawTrackStyledFeature.sourceIdentifier,
geoJSON: .feature(feature))
}
} catch {
NSLog("Error occured while performing operation with source: \(error.localizedDescription).")
}
}
func addStyledFeature(_ styledFeature: StyledFeature) {
do {
let style = navigationMapView.mapView.mapboxMap.style
var source = GeoJSONSource()
source.data = .geometry(.lineString(styledFeature.lineString))
try style.addSource(source, id: styledFeature.sourceIdentifier)
var layer = LineLayer(id: styledFeature.layerIdentifier)
layer.source = styledFeature.sourceIdentifier
layer.lineWidth = .constant(styledFeature.lineWidth)
layer.lineColor = .constant(.init(styledFeature.color))
try style.addPersistentLayer(layer)
} catch {
NSLog("Failed to perform operation with error: \(error.localizedDescription).")
}
}
@objc func didUpdateElectronicHorizonPosition(_ notification: Notification) {
guard let startingEdge = notification.userInfo?[RoadGraph.NotificationUserInfoKey.treeKey] as? RoadGraph.Edge else {
return
}
// Avoid repeating edges that have already been printed out.
guard currentEdgeIdentifier != startingEdge.identifier ||
nextEdgeIdentifier != startingEdge.outletEdges.first?.identifier else {
return
}
currentEdgeIdentifier = startingEdge.identifier
nextEdgeIdentifier = startingEdge.outletEdges.first?.identifier
guard let currentEdgeIdentifier = currentEdgeIdentifier,
let nextEdgeIdentifier = nextEdgeIdentifier else {
return
}
// Identify the current road and upcoming road.
var statusString = "Currently on \(edgeNames(identifier: currentEdgeIdentifier).joined(separator: " / ")), approaching \(edgeNames(identifier: nextEdgeIdentifier).joined(separator: " / "))"
// If there is an upcoming intersection, include the names of the cross streets.
let branchEdgeIdentifiers = startingEdge.outletEdges.suffix(from: 1).map({ $0.identifier })
if !branchEdgeIdentifiers.isEmpty {
let branchNames = branchEdgeIdentifiers.flatMap { edgeNames(identifier: $0) }
statusString += " at \(branchNames.joined(separator: ", "))"
}
// Print the current road, upcoming road, and cross streets.
print(statusString)
}
func edgeNames(identifier: RoadGraph.Edge.Identifier) -> [String] {
if navigationMapView == nil { return [] }
let passiveLocationManager = (navigationMapView.mapView.location.locationProvider as? PassiveLocationProvider)?.locationManager
guard let metadata = passiveLocationManager?.roadGraph.edgeMetadata(edgeIdentifier: identifier) else {
return []
}
let names = metadata.names.map(\.text)
// If the road is unnamed, fall back to the road class.
if names.isEmpty {
return ["\(metadata.mapboxStreetsRoadClass.rawValue)"]
}
return names
}
}