Skip to content

Commit 6501a8e

Browse files
authored
Merge pull request #137 from mapbox/congestion
Congestion
2 parents c7d8449 + 1cee104 commit 6501a8e

File tree

5 files changed

+76
-19
lines changed

5 files changed

+76
-19
lines changed

API.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ The Directions control
2828
- `options.accessToken` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** Required unless `mapboxgl.accessToken` is set globally (optional, default `null`)
2929
- `options.interactive` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** Enable/Disable mouse or touch interactivity from the plugin (optional, default `true`)
3030
- `options.profile` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** Routing profile to use. Options: `driving-traffic`, `driving`, `walking`, `cycling` (optional, default `"driving-traffic"`)
31-
- `options.alternatives` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** Whether to enable alternatives. (optional, default `true`)
31+
- `options.alternatives` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** Whether to enable alternatives. (optional, default `false`)
32+
- `options.congestion` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** Whether to enable congestion along the route line. (optional, default `false`)
3233
- `options.unit` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** Measurement system to be used in navigation instructions. Options: `imperial`, `metric` (optional, default `"imperial"`)
3334
- `options.geocoder` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)?** Pass options available to mapbox-gl-geocoder as [documented here](https://github.com/mapbox/mapbox-gl-geocoder/blob/master/API.md#mapboxglgeocoder).
3435
- `options.controls` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)?**

src/actions/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,14 @@ function setHoverMarker(feature) {
5252

5353
function fetchDirections() {
5454
return (dispatch, getState) => {
55-
const { api, accessToken, routeIndex, profile, alternatives } = getState();
55+
const { api, accessToken, routeIndex, profile, alternatives, congestion } = getState();
5656
const query = buildDirectionsQuery(getState);
5757

5858
// Request params
5959
var options = [];
6060
options.push('geometries=polyline');
6161
if (alternatives) options.push('alternatives=true');
62+
if (congestion) options.push('annotations=congestion');
6263
options.push('steps=true');
6364
options.push('overview=full');
6465
if (accessToken) options.push('access_token=' + accessToken);

src/directions.js

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ import Instructions from './controls/instructions';
2424
* @param {String} [options.accessToken=null] Required unless `mapboxgl.accessToken` is set globally
2525
* @param {Boolean} [options.interactive=true] Enable/Disable mouse or touch interactivity from the plugin
2626
* @param {String} [options.profile="mapbox/driving-traffic"] Routing profile to use. Options: `mapbox/driving-traffic`, `mapbox/driving`, `mapbox/walking`, `mapbox/cycling`
27-
* @param {Boolean} [options.alternatives=true] Whether to enable alternatives.
27+
* @param {Boolean} [options.alternatives=false] Whether to enable alternatives.
28+
* @param {Boolean} [options.congestion=false] Whether to enable congestion along the route line.
2829
* @param {String} [options.unit="imperial"] Measurement system to be used in navigation instructions. Options: `imperial`, `metric`
2930
* @param {Function} [options.compile=null] Provide a custom function for generating instruction, compatible with osrm-text-instructions.
3031
* @param {Object} [options.geocoder] Pass options available to mapbox-gl-geocoder as [documented here](https://github.com/mapbox/mapbox-gl-geocoder/blob/master/API.md#mapboxglgeocoder).
@@ -112,7 +113,7 @@ export default class MapboxDirections {
112113
}
113114

114115
mapState() {
115-
const { profile, alternatives, styles, interactive, compile } = store.getState();
116+
const { profile, alternatives, congestion, styles, interactive, compile } = store.getState();
116117

117118
// Emit any default or option set config
118119
this.actions.eventEmit('profile', { profile });
@@ -167,20 +168,45 @@ export default class MapboxDirections {
167168
if (directions.length) {
168169
directions.forEach((feature, index) => {
169170

170-
const lineString = {
171-
geometry: {
172-
type: 'LineString',
173-
coordinates: decode(feature.geometry, 5).map((c) => {
174-
return c.reverse();
175-
})
176-
},
177-
properties: {
178-
'route-index': index,
179-
route: (index === routeIndex) ? 'selected' : 'alternate'
171+
const features = [];
172+
173+
const decoded = decode(feature.geometry, 5).map(function(c) {
174+
return c.reverse();
175+
});
176+
177+
decoded.forEach(function(c, i) {
178+
var previous = features[features.length - 1];
179+
var congestion = feature.legs[0].annotation && feature.legs[0].annotation.congestion && feature.legs[0].annotation.congestion[i - 1];
180+
181+
if (previous && (!congestion || previous.properties.congestion === congestion)) {
182+
previous.geometry.coordinates.push(c);
183+
} else {
184+
var segment = {
185+
geometry: {
186+
type: 'LineString',
187+
coordinates: []
188+
},
189+
properties: {
190+
'route-index': index,
191+
route: (index === routeIndex) ? 'selected' : 'alternate',
192+
}
193+
};
194+
195+
// New segment starts with previous segment's last coordinate.
196+
if (previous) segment.geometry.coordinates.push(previous.geometry.coordinates[previous.geometry.coordinates.length - 1]);
197+
198+
segment.geometry.coordinates.push(c);
199+
200+
if (congestion) {
201+
segment.properties.congestion = feature.legs[0].annotation.congestion[i - 1];
202+
}
203+
204+
features.push(segment);
180205
}
181-
};
206+
});
207+
208+
geojson.features = geojson.features.concat(features);
182209

183-
geojson.features.push(lineString);
184210
if (index === routeIndex) {
185211
// Collect any possible waypoints from steps
186212
feature.legs[0].steps.forEach((d) => {

src/directions_style.js

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,44 @@ const style = [{
1616
['in', 'route', 'alternate']
1717
]
1818
}, {
19-
'id': 'directions-route-line',
19+
'id': 'directions-route-line-casing',
2020
'type': 'line',
2121
'source': 'directions',
2222
'layout': {
2323
'line-cap': 'round',
2424
'line-join': 'round'
2525
},
2626
'paint': {
27-
'line-color': '#3bb2d0',
28-
'line-width': 4
27+
'line-color': '#2d5f99',
28+
'line-width': 12
29+
},
30+
'filter': [
31+
'all',
32+
['in', '$type', 'LineString'],
33+
['in', 'route', 'selected']
34+
]
35+
}, {
36+
'id': 'directions-route-line',
37+
'type': 'line',
38+
'source': 'directions',
39+
'layout': {
40+
'line-cap': 'butt',
41+
'line-join': 'round'
42+
},
43+
'paint': {
44+
'line-color': {
45+
'property': 'congestion',
46+
'type': 'categorical',
47+
'default': '#4882c5',
48+
'stops': [
49+
['unknown', '#4882c5'],
50+
['low', '#4882c5'],
51+
['moderate', '#f09a46'],
52+
['heavy', '#e34341'],
53+
['severe', '#8b2342']
54+
]
55+
},
56+
'line-width': 7
2957
},
3058
'filter': [
3159
'all',

src/reducers/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const initialState = {
66
api: 'https://api.mapbox.com/directions/v5/',
77
profile: 'mapbox/driving-traffic',
88
alternatives: false,
9+
congestion: false,
910
unit: 'imperial',
1011
compile: null,
1112
proximity: false,

0 commit comments

Comments
 (0)