Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

don't add duplicate layers when custom styles are provided #168

Merged
merged 1 commit into from
Mar 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/directions.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,12 @@ export default class MapboxDirections {
this._map.addSource('directions', geojson);

// Add direction specific styles to the map
directionsStyle.forEach((style) => this._map.addLayer(style));

if (styles && styles.length) styles.forEach((style) => this._map.addLayer(style));
directionsStyle.forEach((style) => {
// only add the default style layer if a custom layer wasn't provided
if (!this._map.getLayer(style.id)) this._map.addLayer(style);
});


if (interactive) {
this._map.on('mousedown', this.onDragDown);
Expand Down
34 changes: 34 additions & 0 deletions test/test.directions.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ function setup() {
"version": 8,
"sources": {
},
"glyphs": "local://glyphs/{fontstack}/{range}.pbf",
"layers": [
{
"id": "background", "type": "background",
Expand Down Expand Up @@ -65,6 +66,39 @@ test('directions', (tt) => {
tt.end();
});

test('Directions with custom styles', t => {
var map = setup();
var customLayer = {
'id': 'directions-route-line',
'type': 'line',
'source': 'directions',
'filter': [
'all',
['in', '$type', 'LineString'],
['in', 'route', 'selected']
],
'layout': {
'line-cap': 'round',
'line-join': 'round'
},
'paint': {
'line-color': '#3bb2d0',
'line-width': 4
}
};
var directions = new MapboxDirections({
styles: [customLayer]
});
t.ok(map.addControl(directions));
map.on('load', ()=>{
t.ok(map.getLayer('directions-route-line-alt'), 'adds default for unspecified custom layer');
t.deepEqual(map.getLayer('directions-route-line').serialize(), customLayer);
})

t.end();
});


test('Directions#onRemove', t => {
var map = setup();
var directions = new MapboxDirections({
Expand Down