Skip to content

Commit b931e4f

Browse files
authored
Merge pull request #2734 from plotly/mapbox-update-layer-fix
Fix mapbox layout layer updates
2 parents 27c77c1 + 52ce74a commit b931e4f

File tree

2 files changed

+60
-3
lines changed

2 files changed

+60
-3
lines changed

src/plots/mapbox/layers.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ proto.update = function update(opts) {
3939
this.updateLayer(opts);
4040
} else if(this.needsNewSource(opts)) {
4141
// IMPORTANT: must delete layer before source to not cause errors
42-
this.updateLayer(opts);
42+
this.removeLayer();
4343
this.updateSource(opts);
44+
this.updateLayer(opts);
4445
} else if(this.needsNewLayer(opts)) {
4546
this.updateLayer(opts);
4647
} else {
@@ -87,8 +88,7 @@ proto.updateLayer = function(opts) {
8788
var map = this.map;
8889
var convertedOpts = convertOpts(opts);
8990

90-
if(map.getLayer(this.idLayer)) map.removeLayer(this.idLayer);
91-
91+
this.removeLayer();
9292
this.layerType = opts.type;
9393

9494
if(isVisible(opts)) {
@@ -111,6 +111,13 @@ proto.updateStyle = function(opts) {
111111
}
112112
};
113113

114+
proto.removeLayer = function() {
115+
var map = this.map;
116+
if(map.getLayer(this.idLayer)) {
117+
map.removeLayer(this.idLayer);
118+
}
119+
};
120+
114121
proto.dispose = function dispose() {
115122
var map = this.map;
116123
map.removeLayer(this.idLayer);

test/jasmine/tests/mapbox_test.js

+50
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,56 @@ describe('@noCI, mapbox plots', function() {
674674
.then(done);
675675
}, LONG_TIMEOUT_INTERVAL);
676676

677+
it('should be able to react to layer changes', function(done) {
678+
function makeFigure(color) {
679+
return {
680+
data: [{type: 'scattermapbox'}],
681+
layout: {
682+
mapbox: {
683+
layers: [{
684+
color: color,
685+
sourcetype: 'geojson',
686+
type: 'fill',
687+
source: {
688+
type: 'Feature',
689+
properties: {},
690+
geometry: {
691+
type: 'Polygon',
692+
coordinates: [[
693+
[174.74475860595703, -36.86533886128865],
694+
[174.77737426757812, -36.86533886128865],
695+
[174.77737426757812, -36.84913134182603],
696+
[174.74475860595703, -36.84913134182603],
697+
[174.74475860595703, -36.86533886128865]
698+
]]
699+
}
700+
}
701+
}]
702+
}
703+
}
704+
};
705+
}
706+
707+
function _assert(color) {
708+
var mapInfo = getMapInfo(gd);
709+
var layer = mapInfo.layers[mapInfo.layoutLayers[0]];
710+
711+
expect(mapInfo.layoutLayers.length).toBe(1, 'one layer');
712+
expect(mapInfo.layoutSources.length).toBe(1, 'one layer source');
713+
expect(String(layer.paint._values['fill-color'].value.value)).toBe(color, 'layer color');
714+
}
715+
716+
Plotly.react(gd, makeFigure('blue')).then(function() {
717+
_assert('rgba(0,0,255,1)');
718+
return Plotly.react(gd, makeFigure('red'));
719+
})
720+
.then(function() {
721+
_assert('rgba(255,0,0,1)');
722+
})
723+
.catch(failTest)
724+
.then(done);
725+
}, LONG_TIMEOUT_INTERVAL);
726+
677727
it('should be able to update the access token', function(done) {
678728
Plotly.relayout(gd, 'mapbox.accesstoken', 'wont-work').catch(function(err) {
679729
expect(gd._fullLayout.mapbox.accesstoken).toEqual('wont-work');

0 commit comments

Comments
 (0)