Skip to content

Commit c0b7829

Browse files
authored
Fix the intersections property loss after route refresh. (#4193)
1 parent bbc4b45 commit c0b7829

File tree

5 files changed

+41
-37
lines changed

5 files changed

+41
-37
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* Added the `NavigationViewController(for:routeIndex:navigationOptions:)` initializer to start turn-by-turn navigation using map matching response. ([#4127](https://github.com/mapbox/mapbox-navigation-ios/pull/4127))
1717
* Fixed an issue where continuous alternative route lines and their corresponding callouts were misplaced on long routes. ([#4176](https://github.com/mapbox/mapbox-navigation-ios/pull/4176))
1818
* Added `ReplayLocationManager(history:)` and `MapboxNavigationService(history:customRoutingProvider:credentials:eventsManagerType:routerType:customActivityType:)` for replaying trips recorded by a history file. ([#4194](https://github.com/mapbox/mapbox-navigation-ios/pull/4194))
19+
* Fixed an issue where after route refresh, the `RouteStepProgress.userDistanceToUpcomingIntersection`, `RouteStepProgress.intersectionsIncludingUpcomingManeuverIntersection`, `RouteStepProgress.intersectionDistances` and `RouteStepProgress.intersectionIndex` are all set to default values. ([#4193](https://github.com/mapbox/mapbox-navigation-ios/pull/4193))
1920

2021
### Map
2122

Sources/MapboxCoreNavigation/RouteController.swift

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ open class RouteController: NSObject {
398398
updateSpokenInstructionProgress(status: status, willReRoute: isRerouting)
399399
updateVisualInstructionProgress(status: status)
400400
updateRoadName(status: status)
401-
updateDistanceToIntersection(from: snappedLocation)
401+
routeProgress.updateDistanceToIntersection(from: snappedLocation)
402402

403403
rerouteAfterArrivalIfNeeded(snappedLocation, status: status)
404404

@@ -408,37 +408,6 @@ open class RouteController: NSObject {
408408
}
409409
}
410410

411-
func updateDistanceToIntersection(from location: CLLocation) {
412-
guard var intersections = routeProgress.currentLegProgress.currentStepProgress.step.intersections else { return }
413-
414-
// The intersections array does not include the upcoming maneuver intersection.
415-
if let upcomingStep = routeProgress.currentLegProgress.upcomingStep,
416-
let upcomingIntersection = upcomingStep.intersections?.first {
417-
intersections += [upcomingIntersection]
418-
}
419-
420-
routeProgress.currentLegProgress.currentStepProgress.intersectionsIncludingUpcomingManeuverIntersection = intersections
421-
422-
if let shape = routeProgress.currentLegProgress.currentStepProgress.step.shape,
423-
let upcomingIntersection = routeProgress.currentLegProgress.currentStepProgress.upcomingIntersection {
424-
routeProgress.currentLegProgress.currentStepProgress.userDistanceToUpcomingIntersection = shape.distance(from: location.coordinate, to: upcomingIntersection.location)
425-
}
426-
427-
updateIntersectionDistances()
428-
}
429-
430-
func updateIntersectionDistances() {
431-
if routeProgress.currentLegProgress.currentStepProgress.intersectionDistances == nil {
432-
routeProgress.currentLegProgress.currentStepProgress.intersectionDistances = [CLLocationDistance]()
433-
434-
if let shape = routeProgress.currentLegProgress.currentStep.shape,
435-
let intersections = routeProgress.currentLegProgress.currentStep.intersections {
436-
let distances: [CLLocationDistance] = intersections.compactMap { shape.distance(from: shape.coordinates.first, to: $0.location) }
437-
routeProgress.currentLegProgress.currentStepProgress.intersectionDistances = distances
438-
}
439-
}
440-
}
441-
442411
@objc func fallbackToOffline(_ notification: Notification) {
443412
updateNavigator(with: indexedRouteResponse, fromLegIndex: self.routeProgress.legIndex, completion: nil)
444413
}

Sources/MapboxCoreNavigation/RouteLegProgress.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,14 +188,14 @@ open class RouteLegProgress: Codable {
188188
- parameter stepIndex: Current step the user is on.
189189
- parameter shapeIndex: Index relative to leg shape, representing the point the user is currently located at.
190190
*/
191-
public init(leg: RouteLeg, stepIndex: Int = 0, spokenInstructionIndex: Int = 0, shapeIndex: Int = 0) {
191+
public init(leg: RouteLeg, stepIndex: Int = 0, spokenInstructionIndex: Int = 0, shapeIndex: Int = 0, intersectionIndex: Int = 0) {
192192
precondition(leg.steps.indices.contains(stepIndex), "It's not possible to set the stepIndex: \(stepIndex) when it's higher than steps count \(leg.steps.count) or not included.")
193193

194194
self.leg = leg
195195
self.stepIndex = stepIndex
196196
self.shapeIndex = shapeIndex
197197

198-
currentStepProgress = RouteStepProgress(step: leg.steps[stepIndex], spokenInstructionIndex: spokenInstructionIndex)
198+
currentStepProgress = RouteStepProgress(step: leg.steps[stepIndex], spokenInstructionIndex: spokenInstructionIndex, intersectionIndex: intersectionIndex)
199199
}
200200

201201
typealias StepIndexDistance = (index: Int, distance: CLLocationDistance)

Sources/MapboxCoreNavigation/RouteProgress.swift

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,11 @@ open class RouteProgress: Codable {
159159
private func commonRefreshRoute(at location: CLLocation) {
160160
currentLegProgress = RouteLegProgress(leg: route.legs[legIndex],
161161
stepIndex: currentLegProgress.stepIndex,
162-
spokenInstructionIndex: currentLegProgress.currentStepProgress.spokenInstructionIndex)
162+
spokenInstructionIndex: currentLegProgress.currentStepProgress.spokenInstructionIndex,
163+
intersectionIndex: currentLegProgress.currentStepProgress.intersectionIndex)
163164
calculateLegsCongestion()
164165
updateDistanceTraveled(with: location)
166+
updateDistanceToIntersection(from: location)
165167
}
166168

167169
/**
@@ -188,6 +190,38 @@ open class RouteProgress: Codable {
188190
}
189191
}
190192

193+
/**
194+
Update the distance to intersection according to new location specified.
195+
- parameter location: Updated user location.
196+
*/
197+
func updateDistanceToIntersection(from location: CLLocation) {
198+
guard var intersections = currentLegProgress.currentStepProgress.step.intersections else { return }
199+
200+
// The intersections array does not include the upcoming maneuver intersection.
201+
if let upcomingIntersection = currentLegProgress.upcomingStep?.intersections?.first {
202+
intersections += [upcomingIntersection]
203+
}
204+
currentLegProgress.currentStepProgress.intersectionsIncludingUpcomingManeuverIntersection = intersections
205+
206+
if let shape = currentLegProgress.currentStep.shape,
207+
let upcomingIntersection = currentLegProgress.currentStepProgress.upcomingIntersection {
208+
currentLegProgress.currentStepProgress.userDistanceToUpcomingIntersection = shape.distance(from: location.coordinate, to: upcomingIntersection.location)
209+
}
210+
211+
updateIntersectionDistances()
212+
}
213+
214+
func updateIntersectionDistances() {
215+
guard currentLegProgress.currentStepProgress.intersectionDistances == nil else { return }
216+
217+
currentLegProgress.currentStepProgress.intersectionDistances = [CLLocationDistance]()
218+
if let shape = currentLegProgress.currentStep.shape,
219+
let intersections = currentLegProgress.currentStep.intersections {
220+
let distances: [CLLocationDistance] = intersections.compactMap { shape.distance(from: shape.coordinates.first, to: $0.location) }
221+
currentLegProgress.currentStepProgress.intersectionDistances = distances
222+
}
223+
}
224+
191225
// MARK: Leg Statistics
192226

193227
/**

Sources/MapboxCoreNavigation/RouteStepProgress.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ open class RouteStepProgress: Codable {
1212

1313
- parameter step: Step on a `RouteLeg`.
1414
*/
15-
public init(step: RouteStep, spokenInstructionIndex: Int = 0) {
15+
public init(step: RouteStep, spokenInstructionIndex: Int = 0, intersectionIndex: Int = 0) {
1616
self.step = step
1717
self.userDistanceToManeuverLocation = step.distance
18-
self.intersectionIndex = 0
1918
self.spokenInstructionIndex = spokenInstructionIndex
19+
self.intersectionIndex = intersectionIndex
2020
}
2121

2222
// MARK: Step Stats

0 commit comments

Comments
 (0)