Skip to content

Commit 1526797

Browse files
authored
Merge pull request #2887 from mapbox/shan/alternative-route-congestion-917
Show congestion levels on alternative route lines
2 parents 8511a7f + 7f9454e commit 1526797

File tree

5 files changed

+38
-10
lines changed

5 files changed

+38
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
* Renamed public property `MBCongestionAttribute` to `CongestionAttribute`. ([#2808](https://github.com/mapbox/mapbox-navigation-ios/pull/2808))
2727
* `NavigationViewController.mapView` was renamed to `NavigationViewController.navigationMapView`. ([#2808](https://github.com/mapbox/mapbox-navigation-ios/pull/2808))
2828
* `NavigationMapView.highlightBuildings(at:in3D:)` was renamed to `NavigationMapView.highlightBuildings(at:in3D:completion:)`. ([#2827](https://github.com/mapbox/mapbox-navigation-ios/pull/2827))
29+
* Add ability to show congestion levels with different colors on alternative route lines, after receiving multiple routes in `RouteResponse` if `NavigationMapView.showsCongestionForAlternativeRoutes` is set to `true`. ([#2887](https://github.com/mapbox/mapbox-navigation-ios/pull/2887))
2930

3031
### Location tracking
3132

Example/ViewController.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ class ViewController: UIViewController {
5757
}
5858

5959
startButton.isEnabled = true
60+
// Show congestion levels on alternative route lines if there're multiple routes in the response.
61+
navigationMapView.showsCongestionForAlternativeRoutes = true
6062
navigationMapView.show(routes)
6163
navigationMapView.showWaypoints(on: currentRoute)
6264
}

Sources/MapboxNavigation/DayStyle.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ extension UIColor {
2929
class var trafficHeavy: UIColor { get { return #colorLiteral(red: 1, green: 0.3019607843, blue: 0.3019607843, alpha: 1) } }
3030
class var trafficSevere: UIColor { get { return #colorLiteral(red: 0.5607843137, green: 0.1411764706, blue: 0.2784313725, alpha: 1) } }
3131

32+
class var alternativeTrafficUnknown: UIColor { get { return defaultAlternateLine } }
33+
class var alternativeTrafficLow: UIColor { get { return defaultAlternateLine } }
34+
class var alternativeTrafficModerate: UIColor { get { return #colorLiteral(red: 0.75, green: 0.63, blue: 0.53, alpha: 1.0) } }
35+
class var alternativeTrafficHeavy: UIColor { get { return #colorLiteral(red: 0.71, green: 0.51, blue: 0.51, alpha: 1.0) } }
36+
class var alternativeTrafficSevere: UIColor { get { return #colorLiteral(red: 0.71, green: 0.51, blue: 0.51, alpha: 1.0) } }
3237
class var defaultBuildingColor: UIColor { get { return #colorLiteral(red: 0.9833194452, green: 0.9843137255, blue: 0.9331936657, alpha: 0.8019049658) } }
3338
class var defaultBuildingHighlightColor: UIColor { get { return #colorLiteral(red: 0.337254902, green: 0.6588235294, blue: 0.9843137255, alpha: 0.949406036) } }
3439
}
@@ -153,6 +158,11 @@ open class DayStyle: Style {
153158
NavigationMapView.appearance().trafficModerateColor = .trafficModerate
154159
NavigationMapView.appearance().trafficSevereColor = .trafficSevere
155160
NavigationMapView.appearance().trafficUnknownColor = .trafficUnknown
161+
NavigationMapView.appearance().alternativeTrafficHeavyColor = .alternativeTrafficHeavy
162+
NavigationMapView.appearance().alternativeTrafficLowColor = .alternativeTrafficLow
163+
NavigationMapView.appearance().alternativeTrafficModerateColor = .alternativeTrafficModerate
164+
NavigationMapView.appearance().alternativeTrafficSevereColor = .alternativeTrafficSevere
165+
NavigationMapView.appearance().alternativeTrafficUnknownColor = .alternativeTrafficUnknown
156166
NavigationMapView.appearance().buildingDefaultColor = .defaultBuildingColor
157167
NavigationMapView.appearance().buildingHighlightColor = .defaultBuildingHighlightColor
158168
NavigationView.appearance().backgroundColor = #colorLiteral(red: 0.764706, green: 0.752941, blue: 0.733333, alpha: 1)

Sources/MapboxNavigation/NavigationMapView+VanishingRouteLine.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ extension NavigationMapView {
217217
})
218218
}
219219

220-
func routeLineGradient(_ route: Route, fractionTraveled: Double) -> [Double: UIColor]? {
220+
func routeLineGradient(_ route: Route, fractionTraveled: Double, isMain: Bool = true) -> [Double: UIColor]? {
221221
var gradientStops = [CGFloat: UIColor]()
222222

223223
/**
@@ -250,7 +250,7 @@ extension NavigationMapView {
250250
// Get congestion color for the stop.
251251

252252
let congestionLevel = feature.properties?[CongestionAttribute] as? String
253-
let associatedCongestionColor = congestionColor(for: congestionLevel)
253+
let associatedCongestionColor = congestionColor(for: congestionLevel, isMain: isMain)
254254

255255
// Measure the line length of the traffic segment.
256256
let lineString = feature.geometry.value as? LineString
@@ -267,7 +267,7 @@ extension NavigationMapView {
267267
gradientStops[segmentEndPercentTraveled.nextDown] = associatedCongestionColor
268268

269269
if index + 1 < congestionSegments.count {
270-
gradientStops[segmentEndPercentTraveled.nextUp] = congestionColor(for: congestionSegments[index + 1].properties?["congestion"] as? String)
270+
gradientStops[segmentEndPercentTraveled.nextUp] = congestionColor(for: congestionSegments[index + 1].properties?["congestion"] as? String, isMain: isMain)
271271
}
272272

273273
continue
@@ -297,7 +297,7 @@ extension NavigationMapView {
297297
gradientStops[segmentEndPercentTraveled.nextDown] = associatedCongestionColor
298298

299299
if index + 1 < congestionSegments.count {
300-
gradientStops[segmentEndPercentTraveled.nextUp] = congestionColor(for: congestionSegments[index + 1].properties?["congestion"] as? String)
300+
gradientStops[segmentEndPercentTraveled.nextUp] = congestionColor(for: congestionSegments[index + 1].properties?["congestion"] as? String, isMain: isMain)
301301
}
302302
}
303303

@@ -343,18 +343,18 @@ extension NavigationMapView {
343343
/**
344344
Given a congestion level, return its associated color.
345345
*/
346-
func congestionColor(for congestionLevel: String?) -> UIColor {
346+
func congestionColor(for congestionLevel: String?, isMain: Bool) -> UIColor {
347347
switch congestionLevel {
348348
case "low":
349-
return trafficLowColor
349+
return isMain ? trafficLowColor : alternativeTrafficLowColor
350350
case "moderate":
351-
return trafficModerateColor
351+
return isMain ? trafficModerateColor : alternativeTrafficModerateColor
352352
case "heavy":
353-
return trafficHeavyColor
353+
return isMain ? trafficHeavyColor : alternativeTrafficHeavyColor
354354
case "severe":
355-
return trafficSevereColor
355+
return isMain ? trafficSevereColor : alternativeTrafficSevereColor
356356
default:
357-
return trafficUnknownColor
357+
return isMain ? trafficUnknownColor : alternativeTrafficUnknownColor
358358
}
359359
}
360360

Sources/MapboxNavigation/NavigationMapView.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ open class NavigationMapView: UIView {
5555
public var roadClassesWithOverriddenCongestionLevels: Set<MapboxStreetsRoadClass>? = nil
5656

5757
var cameraAnimator: CameraAnimator!
58+
/**
59+
Controls whether to show congestion levels on alternative route lines. Defaults to `false`.
60+
61+
If `true` and there're multiple routes to choose, the alternative route lines would display the congestion levels at different colors, similar to the main route. To customize the congestion colors that represent different congestion levels, override the `alternativeTrafficUnknownColor`, `alternativeTrafficLowColor`, `alternativeTrafficModerateColor`, `alternativeTrafficHeavyColor`, `alternativeTrafficSevereColor` property for the `NavigationMapView.appearance()`.
62+
*/
63+
public var showsCongestionForAlternativeRoutes: Bool = false
5864

5965
enum IdentifierType: Int {
6066
case source
@@ -88,6 +94,12 @@ open class NavigationMapView: UIView {
8894
@objc dynamic public var trafficModerateColor: UIColor = .trafficModerate
8995
@objc dynamic public var trafficHeavyColor: UIColor = .trafficHeavy
9096
@objc dynamic public var trafficSevereColor: UIColor = .trafficSevere
97+
@objc dynamic public var alternativeTrafficUnknownColor: UIColor = .alternativeTrafficUnknown
98+
@objc dynamic public var alternativeTrafficLowColor: UIColor = .alternativeTrafficLow
99+
@objc dynamic public var alternativeTrafficModerateColor: UIColor = .alternativeTrafficModerate
100+
@objc dynamic public var alternativeTrafficHeavyColor: UIColor = .alternativeTrafficHeavy
101+
@objc dynamic public var alternativeTrafficSevereColor: UIColor = .alternativeTrafficSevere
102+
91103
@objc dynamic public var routeCasingColor: UIColor = .defaultRouteCasing
92104
@objc dynamic public var routeAlternateColor: UIColor = .defaultAlternateLine
93105
@objc dynamic public var routeAlternateCasingColor: UIColor = .defaultAlternateLineCasing
@@ -635,6 +647,9 @@ open class NavigationMapView: UIView {
635647
lineLayer.layout?.lineJoin = .constant(.round)
636648
lineLayer.layout?.lineCap = .constant(.round)
637649

650+
if showsCongestionForAlternativeRoutes, let gradientStops = routeLineGradient(route, fractionTraveled: 0.0, isMain: false) {
651+
lineLayer.paint?.lineGradient = .expression((Expression.routeLineGradientExpression(gradientStops)))
652+
}
638653
mapView.style.addLayer(layer: lineLayer, layerPosition: LayerPosition(below: parentLayerIndentifier))
639654

640655
return layerIdentifier

0 commit comments

Comments
 (0)