Skip to content

Commit 919fc99

Browse files
committed
【update】map 添加layers时判断是否存在
1 parent 5c30f5f commit 919fc99

1 file changed

Lines changed: 67 additions & 10 deletions

File tree

‎src/mapboxgl/mapping/webmap/v3/WebMap.js‎

Lines changed: 67 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ export class WebMap extends mapboxgl.Evented {
103103
this._sprite = '';
104104
this._spriteDatas = {};
105105
this._appreciableLayers = [];
106+
this._layerCatalogs = [];
106107
}
107108

108109
/**
@@ -250,21 +251,72 @@ export class WebMap extends mapboxgl.Evented {
250251
* @description emit 图层加载成功事件。
251252
*/
252253
_addLayersToMap() {
253-
const { sources, layers } = this._mapInfo;
254+
const { sources, layers, layerIdMapList, layerCataLog } = this._setUniqueId(this._mapInfo);
255+
this._layerCatalogs.push(...layerCataLog)
254256
layers.forEach((layer) => {
255257
layer.source && !this.map.getSource(layer.source) && this.map.addSource(layer.source, sources[layer.source]);
256258
this.map.addLayer(layer);
257259
});
260+
const appreciableLayers = this._generateLayers(layerIdMapList);
261+
this._appreciableLayers.push(...appreciableLayers);
258262
this._sendMapToUser();
259263
}
260264

265+
/**
266+
* @private
267+
* @function WebMap.prototype._setUniqueId
268+
* @description 返回唯一 id 的 sources 和 layers。
269+
* @param {Object} mapInfo - map 信息。
270+
*/
271+
_setUniqueId(mapInfo) {
272+
let layersToMap = JSON.parse(JSON.stringify(mapInfo.layers));
273+
const nextSources = {};
274+
const layerIdToChange = {};
275+
for (let sourceId in mapInfo.sources) {
276+
let timestamp = this.map.getSource(sourceId) ? `_${+new Date()}` : '';
277+
const nextSourceId = sourceId + timestamp;
278+
nextSources[nextSourceId] = mapInfo.sources[sourceId];
279+
layersToMap = layersToMap.map((layer) => {
280+
let nextLayer = layer;
281+
if (layer.source === sourceId) {
282+
let layerId = layer.id;
283+
if (this.map.getLayer(layerId)) {
284+
layerId = timestamp ? layer.id + timestamp : `${layer.id}_${+new Date()}`;
285+
}
286+
nextLayer = Object.assign({}, layer, { id: layerId, source: nextSourceId });
287+
}
288+
layerIdToChange[layer.id] = [nextLayer.id, nextLayer.source];
289+
return nextLayer;
290+
});
291+
}
292+
const layerCataLog = JSON.parse(JSON.stringify(mapInfo.metadata.layerCatalog));
293+
this._updateLayerCatalogsId(layerCataLog, layerIdToChange);
294+
return {
295+
sources: nextSources,
296+
layers: layersToMap,
297+
layerIdMapList: layerIdToChange,
298+
layerCataLog
299+
};
300+
}
301+
302+
_updateLayerCatalogsId(catalogs, layerIdMapList) {
303+
catalogs.forEach((catalog) => {
304+
const { id, children } = catalog;
305+
if (catalog.type === 'group') {
306+
this._updateLayerCatalogsId(children, layerIdMapList);
307+
return;
308+
}
309+
catalog.originId = id;
310+
catalog.id = layerIdMapList[id][0];
311+
});
312+
}
313+
261314
/**
262315
* @private
263316
* @function WebMap.prototype._sendMapToUser
264317
* @description emit 图层加载成功事件。
265318
*/
266319
_sendMapToUser() {
267-
this._appreciableLayers = this._generateLayers();
268320
this.fire('addlayerssucceeded', { map: this.map, mapparams: this.mapParams, layers: this._appreciableLayers });
269321
}
270322

@@ -292,26 +344,32 @@ export class WebMap extends mapboxgl.Evented {
292344
this.map.remove();
293345
this.map = null;
294346
this._legendList = [];
347+
this._mapResourceInfo = {};
348+
this._sprite = '';
349+
this._spriteDatas = {};
295350
this.mapOptions = {};
296351
this.options = {};
297352
this._appreciableLayers = [];
353+
this._layerCatalogs = [];
298354
}
299355
}
300356

301357
/**
302358
* @private
303359
* @function WebMap.prototype._generateV2LayersStructure
304360
* @description emit 图层加载成功事件。
361+
* @param {Array<Object>} layerIdRenameMapList - 图层信息。
305362
*/
306-
_generateLayers() {
363+
_generateLayers(layerIdRenameMapList) {
307364
const { catalogs = [] } = this._mapResourceInfo;
308365
const originLayers = this._getLayerInfosFromCatalogs(catalogs);
309366
const layers = originLayers.map((layer) => {
310-
const { id, title, visualization, visible, layersContent } = layer;
367+
const { title, visualization, visible, layersContent } = layer;
368+
const [realLayerId, realSourceId] = layerIdRenameMapList[layer.id];
311369
const layerFromMapInfo = this._mapInfo.layers.find((item) => {
312370
return item.id === layer.id;
313371
});
314-
this._createLegendInfo(Object.assign({}, layerFromMapInfo, { title: layer.title }), visualization);
372+
this._createLegendInfo(Object.assign({}, layerFromMapInfo, { title: layer.title, id: realLayerId }), visualization);
315373
let dataType = '';
316374
let dataId = '';
317375
for (const data of this._mapResourceInfo.datas) {
@@ -327,16 +385,16 @@ export class WebMap extends mapboxgl.Evented {
327385
serverId: dataId,
328386
type: dataType
329387
},
330-
id,
388+
id: realLayerId,
331389
type: layerFromMapInfo.type,
332390
title,
333391
visible,
334392
renderSource: {
335-
id: layerFromMapInfo.source,
393+
id: realSourceId,
336394
type: layerFromMapInfo.type === 'raster' ? 'raster' : 'vector',
337395
sourceLayer: layerFromMapInfo['source-layer']
338396
},
339-
renderLayers: this._getRenderLayers(layersContent, id)
397+
renderLayers: this._getRenderLayers(layersContent, realLayerId)
340398
});
341399
const styleSettings = this._parseRendererStyleData(visualization.renderer);
342400
const defaultStyleSetting = styleSettings[0];
@@ -360,8 +418,7 @@ export class WebMap extends mapboxgl.Evented {
360418
}
361419

362420
getLayerCatalog() {
363-
const originLayerCatalog = this._mapInfo.metadata.layerCatalog;
364-
const formatLayerCatalog = this._createFormatCatalogs(originLayerCatalog);
421+
const formatLayerCatalog = this._createFormatCatalogs(this._layerCatalogs);
365422
const baseLayer = this._getBaseLayer(formatLayerCatalog);
366423
return baseLayer ? [baseLayer].concat(formatLayerCatalog) : formatLayerCatalog;
367424
}

0 commit comments

Comments
 (0)