diff --git a/src/mapboxgl/web-map/WebMapV2.ts b/src/mapboxgl/web-map/WebMapV2.ts index 1c07a3b0..04b1629c 100644 --- a/src/mapboxgl/web-map/WebMapV2.ts +++ b/src/mapboxgl/web-map/WebMapV2.ts @@ -2572,6 +2572,11 @@ export default class WebMap extends WebMapBase { default: break; } + const matchIndex = this._legendList.findIndex(item => item.layerId === layerInfo.layerID); + if (matchIndex > -1) { + this._legendList.splice(matchIndex, 1, legendItem); + return; + } this._legendList.push(legendItem); } diff --git a/src/mapboxgl/web-map/__tests__/WebMapViewModel.spec.js b/src/mapboxgl/web-map/__tests__/WebMapViewModel.spec.js index 84f0592c..32230581 100644 --- a/src/mapboxgl/web-map/__tests__/WebMapViewModel.spec.js +++ b/src/mapboxgl/web-map/__tests__/WebMapViewModel.spec.js @@ -1519,6 +1519,7 @@ describe('WebMapViewModel.spec', () => { const viewModel = new WebMapViewModel(commonId, { ...commonOption }, { ...commonMapOptions }, { ...commonMap }); const callback = function (data) { expect(viewModel.getAppreciableLayers().length).toBe(uniqueLayer_polygon.layers.length + 1); + expect(viewModel.getLegendInfo().length).toBe(2); const layerInfo = { ...uniqueLayer_polygon.layers[0], id: uniqueLayer_polygon.layers[0].name }; const features = [ { @@ -1547,6 +1548,7 @@ describe('WebMapViewModel.spec', () => { const spy = jest.spyOn(viewModel._handler, '_initOverlayLayer'); viewModel.updateOverlayLayer(layerInfo, features); expect(spy).toBeCalled(); + expect(viewModel.getLegendInfo().length).toBe(2); done(); }; viewModel.on({ addlayerssucceeded: callback });