Skip to content

Commit 8ffd203

Browse files
committed
Add basic lane support.
1 parent 6501a8e commit 8ffd203

File tree

3 files changed

+81
-7
lines changed

3 files changed

+81
-7
lines changed

src/controls/instructions.js

Lines changed: 68 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,76 @@ export default class Instructions {
3939
if (directions.length && shouldRender) {
4040
const direction = this.directions = directions[routeIndex];
4141

42-
if (compile) {
43-
direction.legs.forEach(function(leg) {
44-
leg.steps.forEach(function(step) {
42+
direction.legs.forEach(function(leg) {
43+
leg.steps.forEach(function(step) {
44+
step.lanes = stepToLanes(step);
45+
if (compile) {
4546
step.maneuver.instruction = compile('en', step);
46-
});
47+
}
4748
});
48-
}
49+
});
50+
51+
function stepToLanes(step) {
52+
var lanes = step.intersections[0].lanes;
53+
54+
if (!lanes) return [];
55+
56+
var maneuver = step.maneuver.modifier || '';
57+
58+
return lanes.map(function(lane, index) {
59+
var classes = [];
60+
if (!lane.valid) classes.push(['invalid']);
61+
62+
// check maneuver direction matches this lane one(s)
63+
var maneuverIndication = lane.indications.indexOf(maneuver);
64+
if (maneuverIndication === -1) {
65+
// check non-indicated lane to allow straight, right turn from last lane and left turn for first lane
66+
if ((lane.indications[0] === 'none' || lane.indications[0] === '') && (
67+
maneuver === 'straight' ||
68+
(index === 0 && maneuver.slice(-4) === 'left') ||
69+
(index === (lanes.length - 1) && maneuver.slice(-5) === 'right'))) {
70+
maneuverIndication = 0;
71+
} else if (maneuver.slice(0, 7) === 'slight ' ) {
72+
// try to exclude 'slight' modifier
73+
maneuverIndication = lane.indications.indexOf(maneuver.slice(7));
74+
}
75+
}
76+
var indication = (maneuverIndication === -1) ? lane.indications[0] : maneuver;
77+
78+
var icon;
79+
switch (indication) {
80+
case 'right':
81+
icon = 'right';
82+
break;
83+
case 'sharp right':
84+
icon = 'sharp-right';
85+
break;
86+
case 'slight right':
87+
icon = 'slight-right';
88+
break;
89+
case 'left':
90+
icon = 'left';
91+
break;
92+
case 'sharp left':
93+
icon = 'sharp-left';
94+
break;
95+
case 'slight left':
96+
icon = 'slight-left';
97+
break;
98+
case 'uturn':
99+
icon = 'u-turn';
100+
break;
101+
//case 'straight':
102+
//case 'none':
103+
default:
104+
icon = 'straight';
105+
break;
106+
}
107+
classes.push('directions-icon-' + icon);
108+
109+
return classes.join(' ');
110+
});
111+
}
49112

50113
this.container.innerHTML = instructionsTemplate({
51114
routeIndex,

src/mapbox-gl-directions.css

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@
354354
}
355355

356356
/* Instructions */
357-
.mapbox-directions-instructions .directions-icon {
357+
.mapbox-directions-instructions .directions-maneuver-icon {
358358
position:absolute;
359359
left:12px;
360360
top:20px;
@@ -459,6 +459,10 @@
459459
speak:none;
460460
background-repeat:no-repeat;
461461
}
462+
463+
.directions-icon.invalid {
464+
opacity: 0.25;
465+
}
462466
.directions-icon-arrive {
463467
background-image:url(data:image/svg+xml;base64,PHN2ZyB4bWxuczpzdmc9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIyMCIgaGVpZ2h0PSIyMCIgaWQ9InN2ZzUzMjciIHZlcnNpb249IjEuMSIgdmlld0JveD0iMCAwIDIwIDIwIj48ZGVmcyBpZD0iZGVmczUzMjkiLz48bWV0YWRhdGEgaWQ9Im1ldGFkYXRhNTMzMiIvPjxnIGlkPSJsYXllcjEiIHRyYW5zZm9ybT0idHJhbnNsYXRlKC0zMjAwLC05OTIuMzYyMTgpIj48ZyBpZD0iZzU5MzYiIHRyYW5zZm9ybT0ibWF0cml4KDEsMCwwLC0xLDEsMjAwNC43MjQ0KSIgb3BhY2l0eT0iMC41Ii8+PGcgaWQ9Imc1OTMyIiB0cmFuc2Zvcm09InRyYW5zbGF0ZSgxLC0yKSIvPjxnIHRyYW5zZm9ybT0ibWF0cml4KDEsMCwwLC0xLDEsMjAyNC43M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AwIDMuMTMgMS42NiAwcTEuMDUgMCAxLjU1LTAuNCAwLjUtMC40IDAuNS0xLjIzIDAtMS40OS0yLjEzLTEuNDlsLTEuNTggMHoiIGlkPSJwYXRoMjcyMTIiLz48L2c+PC9nPjwvc3ZnPg==);
464468
}

src/templates/instructions.html

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,17 @@ <h1><%- duration %></h1>
3535
data-lat='<%= lat %>'
3636
data-lng='<%= lng %>'
3737
class='mapbox-directions-step'>
38-
<span class='directions-icon directions-icon-<%= icon %>'></span>
38+
<span class='directions-icon directions-maneuver-icon directions-icon-<%= icon %>'></span>
3939
<div class='mapbox-directions-step-maneuver'>
4040
<%= step.maneuver.instruction %>
4141
</div>
42+
<% if (step.lanes) { %>
43+
<div class='mapbox-directions-lanes'>
44+
<% step.lanes.forEach(function(lane) { %>
45+
<span class='directions-icon directions-lane-icon <%- lane %>'></span>
46+
<% }); %>
47+
</div>
48+
<% } %>
4249
<% if (distance) { %>
4350
<div class='mapbox-directions-step-distance'>
4451
<%= distance %>

0 commit comments

Comments
 (0)