Skip to content

Commit 31c7911

Browse files
authored
don't add duplicate layers when custom styles are provided (#168)
1 parent 40b9ffb commit 31c7911

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

src/directions.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,12 @@ export default class MapboxDirections {
137137
this._map.addSource('directions', geojson);
138138

139139
// Add direction specific styles to the map
140-
directionsStyle.forEach((style) => this._map.addLayer(style));
141-
142140
if (styles && styles.length) styles.forEach((style) => this._map.addLayer(style));
141+
directionsStyle.forEach((style) => {
142+
// only add the default style layer if a custom layer wasn't provided
143+
if (!this._map.getLayer(style.id)) this._map.addLayer(style);
144+
});
145+
143146

144147
if (interactive) {
145148
this._map.on('mousedown', this.onDragDown);

test/test.directions.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ function setup() {
1010
"version": 8,
1111
"sources": {
1212
},
13+
"glyphs": "local://glyphs/{fontstack}/{range}.pbf",
1314
"layers": [
1415
{
1516
"id": "background", "type": "background",
@@ -65,6 +66,39 @@ test('directions', (tt) => {
6566
tt.end();
6667
});
6768

69+
test('Directions with custom styles', t => {
70+
var map = setup();
71+
var customLayer = {
72+
'id': 'directions-route-line',
73+
'type': 'line',
74+
'source': 'directions',
75+
'filter': [
76+
'all',
77+
['in', '$type', 'LineString'],
78+
['in', 'route', 'selected']
79+
],
80+
'layout': {
81+
'line-cap': 'round',
82+
'line-join': 'round'
83+
},
84+
'paint': {
85+
'line-color': '#3bb2d0',
86+
'line-width': 4
87+
}
88+
};
89+
var directions = new MapboxDirections({
90+
styles: [customLayer]
91+
});
92+
t.ok(map.addControl(directions));
93+
map.on('load', ()=>{
94+
t.ok(map.getLayer('directions-route-line-alt'), 'adds default for unspecified custom layer');
95+
t.deepEqual(map.getLayer('directions-route-line').serialize(), customLayer);
96+
})
97+
98+
t.end();
99+
});
100+
101+
68102
test('Directions#onRemove', t => {
69103
var map = setup();
70104
var directions = new MapboxDirections({

0 commit comments

Comments
 (0)