Skip to content

Commit 6b31415

Browse files
authored
Add the ability to show guidance view images on CarPlay. (#4324)
1 parent 0fd021e commit 6b31415

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@
4545

4646
* Fixed a rare crash that could happen during route simulation. ([#4153](https://github.com/mapbox/mapbox-navigation-ios/pull/4153))
4747

48+
### CarPlay
49+
50+
* Added the ability to display a junction image for the maneuver on CarPlay. ([#4324](https://github.com/mapbox/mapbox-navigation-ios/pull/4324))
51+
4852
### Other changes
4953

5054
* Fixed potential issue when rerouting and route refreshing happen simultaneously which could lead to old route restoration or even a crash. ([#4238](https://github.com/mapbox/mapbox-navigation-ios/pull/4238))

Example/ViewController+FreeDrive.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ extension ViewController {
153153
}
154154

155155
func edgeNames(identifier: RoadGraph.Edge.Identifier) -> [String] {
156+
if navigationMapView == nil { return [] }
156157
let passiveLocationManager = (navigationMapView.mapView.location.locationProvider as? PassiveLocationProvider)?.locationManager
157158
guard let metadata = passiveLocationManager?.roadGraph.edgeMetadata(edgeIdentifier: identifier) else {
158159
return []

Sources/MapboxNavigation/CarPlayNavigationViewController.swift

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,10 @@ open class CarPlayNavigationViewController: UIViewController, BuildingHighlighti
916916
primaryManeuver.symbolSet = visualInstruction.primaryInstruction.maneuverImageSet(side: visualInstruction.drivingSide)
917917
}
918918

919+
let junctionImage = guidanceViewManeuverRepresentation(for: visualInstruction,
920+
navigationService: navigationService)
921+
primaryManeuver.junctionImage = junctionImage
922+
919923
// Estimating the width of Apple's maneuver view
920924
let bounds: () -> (CGRect) = {
921925
let widthOfManeuverView = min(self.view.bounds.width - self.view.safeArea.left,
@@ -1006,6 +1010,42 @@ open class CarPlayNavigationViewController: UIViewController, BuildingHighlighti
10061010
carSession.upcomingManeuvers = maneuvers
10071011
}
10081012

1013+
/**
1014+
Returns guidance view image representation if it's present in the current visual instruction.
1015+
Since CarPlay doesn't support asynchronous maneuvers update, in case if guidance view image is
1016+
not present in cache - download guidance image first and after that trigger maneuvers update.
1017+
In case if image is present in cache - update primary maneuver right away.
1018+
*/
1019+
func guidanceViewManeuverRepresentation(for visualInstruction: VisualInstructionBanner?,
1020+
navigationService: NavigationService) -> UIImage? {
1021+
guard let quaternaryInstruction = visualInstruction?.quaternaryInstruction,
1022+
let guidanceView = quaternaryInstruction.components.first,
1023+
let cacheKey = guidanceView.cacheKey else {
1024+
return nil
1025+
}
1026+
1027+
if let cachedImage = ImageRepository.shared.cachedImageForKey(cacheKey) {
1028+
return cachedImage
1029+
} else {
1030+
guard case let .guidanceView(guidanceViewImageRepresentation, _, _) = guidanceView,
1031+
let guidanceImageURL = guidanceViewImageRepresentation.imageURL,
1032+
let accessToken = navigationService.credentials.accessToken,
1033+
let guidanceViewImageURL = URL(string: guidanceImageURL.absoluteString + "&access_token=" + accessToken) else {
1034+
return nil
1035+
}
1036+
1037+
ImageRepository.shared.imageWithURL(guidanceViewImageURL,
1038+
cacheKey: cacheKey) { [weak self] _ in
1039+
DispatchQueue.main.async {
1040+
guard let self = self else { return }
1041+
self.updateManeuvers(navigationService.routeProgress)
1042+
}
1043+
}
1044+
1045+
return nil
1046+
}
1047+
}
1048+
10091049
func presentWaypointArrivalUI(for waypoint: Waypoint) {
10101050
var title = NSLocalizedString("CARPLAY_ARRIVED",
10111051
bundle: .mapboxNavigation,

0 commit comments

Comments
 (0)