Skip to content

Commit 1cee104

Browse files
committed
Segments that share congestion share a feature.
1 parent d312dca commit 1cee104

File tree

1 file changed

+35
-18
lines changed

1 file changed

+35
-18
lines changed

src/directions.js

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -168,28 +168,45 @@ export default class MapboxDirections {
168168
if (directions.length) {
169169
directions.forEach((feature, index) => {
170170

171-
const decoded = decode(feature.geometry, 5);
172-
decoded.forEach((c, i) => {
173-
if (i === 0) {
174-
c.reverse();
175-
return;
176-
}
177-
var segment = {
178-
geometry: {
179-
type: 'LineString',
180-
coordinates: [decoded[i - 1], c.reverse()]
181-
},
182-
properties: {
183-
'route-index': index,
184-
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];
185202
}
186-
};
187-
if (feature.legs[0].annotation && feature.legs[0].annotation.congestion) {
188-
segment.properties.congestion = feature.legs[0].annotation.congestion[i - 1];
203+
204+
features.push(segment);
189205
}
190-
geojson.features.push(segment);
191206
});
192207

208+
geojson.features = geojson.features.concat(features);
209+
193210
if (index === routeIndex) {
194211
// Collect any possible waypoints from steps
195212
feature.legs[0].steps.forEach((d) => {

0 commit comments

Comments
 (0)