Skip to content

Commit d6dfe12

Browse files
author
Bobby Sudekum
authored
Improve departure announcement (#654)
1 parent 1ee18b0 commit d6dfe12

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

MapboxCoreNavigation/Constants.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,8 @@ let milesToMeters = 1609.34
150150
The mimimum speed value before the user is snapped to the route. This is used to overcome inaccurate course values when a user's speed is low.
151151
*/
152152
public var RouteControllerMinimumSpeedThresholdForSnappingUserToRoute: CLLocationSpeed = 2
153+
154+
/**
155+
The minimum distance threshold used for giving a "Continue" type instructions.
156+
*/
157+
public var RouteControllerMinDistanceForContinueInstruction: CLLocationDistance = 2_000

MapboxCoreNavigation/SpokenInstructionFormatter.swift

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,34 @@ public class SpokenInstructionFormatter: NSObject {
7272
// We only want to announce this special depature announcement once.
7373
// Once it has been announced, all subsequnt announcements will not have an alert level of low
7474
// since the user will be approaching the maneuver location.
75-
let isDeparture = routeProgress.currentLegProgress.currentStep.maneuverType == .depart && alertLevel == .depart
76-
let didCompleteManeuver = routeProgress.currentLegProgress.currentStep.distance > 2_000 && routeProgress.currentLegProgress.alertUserLevel == .low
77-
if isDeparture || didCompleteManeuver {
78-
if isDeparture && upcomingStepDuration < linkedInstructionMultiplier {
75+
let isStartingDeparture = routeProgress.currentLegProgress.currentStep.maneuverType == .depart && (alertLevel == .depart || alertLevel == .low)
76+
if let currentInstruction = currentInstruction, isStartingDeparture {
77+
if routeProgress.currentLegProgress.currentStep.distance > RouteControllerMinDistanceForContinueInstruction {
78+
text = currentInstruction
79+
} else if upcomingStepDuration > linkedInstructionMultiplier {
80+
// If the upcoming step is an .exitRoundabout or .exitRotary, don't link the instruction
81+
if let followOnStep = routeProgress.currentLegProgress.followOnStep, followOnStep.maneuverType == .exitRoundabout || followOnStep.maneuverType == .exitRotary {
82+
text = upComingInstruction
83+
} else {
84+
let phrase = escapeIfNecessary(routeStepFormatter.instructions.phrase(named: .twoInstructionsWithDistance))
85+
text = phrase.replacingTokens { (tokenType) -> String in
86+
switch tokenType {
87+
case .firstInstruction:
88+
return currentInstruction
89+
case .secondInstruction:
90+
return upComingInstruction
91+
case .distance:
92+
return maneuverVoiceDistanceFormatter.string(from: userDistance)
93+
default:
94+
fatalError("Unexpected token \(tokenType)")
95+
}
96+
}
97+
}
98+
} else {
99+
text = upComingInstruction
100+
}
101+
} else if routeProgress.currentLegProgress.currentStep.distance > RouteControllerMinDistanceForContinueInstruction && routeProgress.currentLegProgress.alertUserLevel == .low {
102+
if isStartingDeparture && upcomingStepDuration < linkedInstructionMultiplier {
79103
let phrase = escapeIfNecessary(routeStepFormatter.instructions.phrase(named: .twoInstructionsWithDistance))
80104
text = phrase.replacingTokens { (tokenType) -> String in
81105
switch tokenType {

0 commit comments

Comments
 (0)