Skip to content

Commit d392194

Browse files
authored
Alternatives callouts and route line display (#4176)
* vk-navios-365-alternatives-callouts: fixed alternative routes callouts positioning and it's route line vanishing before the deviation point; fixed incorrect relative duration text in a callout; CHANGELOG updated
1 parent 24d6403 commit d392194

File tree

3 files changed

+41
-12
lines changed

3 files changed

+41
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
* Added the `RouteController.prefersOnlineRoute` property, which lets you automatically switch from a route generated on the device to one generated by the Mapbox Directions API if the geometries match. Use `NavigationViewControllerDelegate.navigationViewController(_:didSwitchToCoincidentOnlineRoute:)`, `NavigationServiceDelegate.navigationService(_:didSwitchToCoincidentOnlineRoute:)`, `RouterDelegate.router(_:didSwitchToCoincidentOnlineRoute:)` or `.routeControllerDidSwitchToCoincidentOnlineRoute` notification to track such events. ([#4127](https://github.com/mapbox/mapbox-navigation-ios/pull/4127))
1414
* 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))
15+
* 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))
1516

1617
### Map
1718

Sources/MapboxNavigation/NavigationMapView+VanishingRouteLine.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,23 @@ extension NavigationMapView {
122122
routeRemainingDistancesIndex = allPoints - allRemainingPoints
123123
}
124124

125+
func calculateGranularDistanceOffset(_ coordinates: [CLLocationCoordinate2D], splitPoint: CLLocationCoordinate2D) -> Double? {
126+
if coordinates.isEmpty { return nil }
127+
var distance = 0.0
128+
var closestError = Double.greatestFiniteMagnitude
129+
var pointDistance: Double? = nil
130+
for index in stride(from: coordinates.count - 1, to: 0, by: -1) {
131+
let curr = coordinates[index]
132+
let prev = coordinates[index - 1]
133+
distance += curr.projectedDistance(to: prev)
134+
if curr.distance(to: splitPoint) < closestError {
135+
pointDistance = distance
136+
closestError = curr.distance(to: splitPoint)
137+
}
138+
}
139+
return pointDistance.map { (distance - $0) / distance }
140+
}
141+
125142
func calculateGranularDistances(_ coordinates: [CLLocationCoordinate2D]) -> RouteLineGranularDistances? {
126143
if coordinates.isEmpty { return nil }
127144
var distance = 0.0

Sources/MapboxNavigation/NavigationMapView.swift

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -303,12 +303,14 @@ open class NavigationMapView: UIView {
303303
}
304304
}
305305

306-
continuousAlternatives?.forEach { routeAlternative in
307-
guard let route = routeAlternative.indexedRouteResponse.currentRoute else {
306+
guard let continuousAlternatives = continuousAlternatives else { return }
307+
for (index, routeAlternative) in continuousAlternatives.enumerated() {
308+
guard let route = routeAlternative.indexedRouteResponse.currentRoute,
309+
alternativesRouteLineDeviationOffsets?.count ?? 0 > index,
310+
let offset = alternativesRouteLineDeviationOffsets?[index] else {
308311
return
309312
}
310313

311-
let offset = (route.distance - routeAlternative.infoFromDeviationPoint.distance) / route.distance
312314
parentLayerIdentifier = addRouteLayer(route,
313315
below: parentLayerIdentifier,
314316
isMainRoute: false,
@@ -1152,14 +1154,11 @@ open class NavigationMapView: UIView {
11521154
var features = [Turf.Feature]()
11531155

11541156
for (index, alternativeRoute) in routes.enumerated() {
1155-
guard let routeShape = alternativeRoute.indexedRouteResponse.currentRoute?.shape else { return }
1156-
1157-
var annotationCoordinate: LocationCoordinate2D!
1158-
if let mainRoute = self.routes?.first {
1159-
let offset = mainRoute.distance - alternativeRoute.infoFromDeviationPoint.distance + continuousAlternativeDurationAnnotationOffset
1160-
annotationCoordinate = routeShape.indexedCoordinateFromStart(distance: offset)?.coordinate
1161-
} else {
1162-
annotationCoordinate = routeShape.indexedCoordinateFromStart(distance: alternativeRoute.infoFromOrigin.distance - alternativeRoute.infoFromDeviationPoint.distance + continuousAlternativeDurationAnnotationOffset)?.coordinate
1157+
guard let routeShape = alternativeRoute.indexedRouteResponse.currentRoute?.shape,
1158+
let annotationCoordinate = routeShape.indexedCoordinateFromStart(distance: alternativeRoute.infoFromOrigin.distance
1159+
- alternativeRoute.infoFromDeviationPoint.distance
1160+
+ continuousAlternativeDurationAnnotationOffset)?.coordinate else {
1161+
return
11631162
}
11641163

11651164
// Form the appropriate text string for the annotation.
@@ -1693,7 +1692,19 @@ open class NavigationMapView: UIView {
16931692
// MARK: Map Rendering and Observing
16941693

16951694
var routes: [Route]?
1696-
var continuousAlternatives: [AlternativeRoute]?
1695+
var continuousAlternatives: [AlternativeRoute]? {
1696+
didSet {
1697+
alternativesRouteLineDeviationOffsets = continuousAlternatives?.map {
1698+
guard let coordinates = $0.indexedRouteResponse.currentRoute?.shape?.coordinates,
1699+
let projectedOffset = calculateGranularDistanceOffset(coordinates,
1700+
splitPoint: $0.alternativeRouteIntersection.location) else {
1701+
return 0.0
1702+
}
1703+
return projectedOffset
1704+
}
1705+
}
1706+
}
1707+
var alternativesRouteLineDeviationOffsets: [Double]?
16971708
var routePoints: RoutePoints?
16981709
var routeLineGranularDistances: RouteLineGranularDistances?
16991710
var routeRemainingDistancesIndex: Int?

0 commit comments

Comments
 (0)