Skip to content

Commit d3fbdfd

Browse files
committed
【feature】webmap 加载 L7 优化
1 parent 0d1e413 commit d3fbdfd

3 files changed

Lines changed: 53 additions & 34 deletions

File tree

src/mapboxgl/mapping/utils/L7LayerUtil.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1727,6 +1727,11 @@ function createG2ChartDataByFeature(fields, feature, hasPercent = false) {
17271727
* @param {g2ChartItemData[]} itemData
17281728
*/
17291729
function createG2Chart(el, width, height, itemData) {
1730+
if (!G2) {
1731+
const errorMsg = 'G2 is not define';
1732+
console.error(errorMsg);
1733+
throw new Error(errorMsg);
1734+
}
17301735
const chart = new G2.Chart({
17311736
container: el,
17321737
width,

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

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -276,34 +276,38 @@ export class WebMap extends mapboxgl.Evented {
276276
* @description emit 图层加载成功事件。
277277
*/
278278
async _addLayersToMap() {
279-
const { sources, layers, layerCatalog, catalogs } = this._setUniqueId(this._mapInfo, this._mapResourceInfo);
280-
Object.assign(this._mapInfo, {
281-
sources,
282-
layers,
283-
metadata: Object.assign(this._mapInfo.metadata, { layerCatalog })
284-
});
285-
Object.assign(this._mapResourceInfo, { catalogs });
286-
const mapboxglLayers = layers.filter((layer) => !isMapboxUnSupportLayer(layer));
287-
mapboxglLayers.forEach((layer) => {
288-
layer.source && !this.map.getSource(layer.source) && this.map.addSource(layer.source, sources[layer.source]);
289-
// L7才会用到此属性
290-
if (layer.type === 'symbol' && (layer.layout || {})['text-z-offset'] === 0) {
291-
delete layer.layout['text-z-offset'];
292-
}
293-
this.map.addLayer(layer);
294-
});
295-
const l7Layers = layers.filter((layer) => isMapboxUnSupportLayer(layer));
296-
if (l7Layers.length > 0) {
297-
await addL7Layers({
298-
map: this.map,
299-
webMapInfo: { ...this._mapInfo, layers, sources },
300-
l7Layers,
301-
spriteDatas: this._spriteDatas,
302-
options: this.options
279+
try {
280+
const { sources, layers, layerCatalog, catalogs } = this._setUniqueId(this._mapInfo, this._mapResourceInfo);
281+
Object.assign(this._mapInfo, {
282+
sources,
283+
layers,
284+
metadata: Object.assign(this._mapInfo.metadata, { layerCatalog })
303285
});
286+
Object.assign(this._mapResourceInfo, { catalogs });
287+
const mapboxglLayers = layers.filter((layer) => !isMapboxUnSupportLayer(layer));
288+
mapboxglLayers.forEach((layer) => {
289+
layer.source && !this.map.getSource(layer.source) && this.map.addSource(layer.source, sources[layer.source]);
290+
// L7才会用到此属性
291+
if (layer.type === 'symbol' && (layer.layout || {})['text-z-offset'] === 0) {
292+
delete layer.layout['text-z-offset'];
293+
}
294+
this.map.addLayer(layer);
295+
});
296+
const l7Layers = layers.filter((layer) => isMapboxUnSupportLayer(layer));
297+
if (l7Layers.length > 0) {
298+
await addL7Layers({
299+
map: this.map,
300+
webMapInfo: { ...this._mapInfo, layers, sources },
301+
l7Layers,
302+
spriteDatas: this._spriteDatas,
303+
options: this.options
304+
});
305+
}
306+
this._createLegendInfo();
307+
this._sendMapToUser();
308+
} catch (error) {
309+
this.fire('getlayersfailed', { error, map: this.map });
304310
}
305-
this._createLegendInfo();
306-
this._sendMapToUser();
307311
}
308312

309313
/**
@@ -553,7 +557,7 @@ export class WebMap extends mapboxgl.Evented {
553557
_generateLayerCatalog() {
554558
const { layerCatalog } = this._mapInfo.metadata;
555559
const layerIdsFromCatalog = layerCatalog.reduce((ids, item) => {
556-
const list = this._collectChildrenKey([item], 'id');
560+
const list = this._collectChildrenKey([item], ['id', 'parts']);
557561
ids.push(...list);
558562
return ids;
559563
}, []);
@@ -602,18 +606,28 @@ export class WebMap extends mapboxgl.Evented {
602606

603607
_updateLayerVisible(catalogs) {
604608
for (const data of catalogs) {
605-
const list = this._collectChildrenKey([data], 'visible');
609+
const list = this._collectChildrenKey([data], ['visible']);
606610
data.visible = list.every((item) => item);
607611
}
608612
}
609613

610-
_collectChildrenKey(catalogs, key, list = []) {
614+
_collectChildrenKey(catalogs, keys, list = []) {
611615
for (const data of catalogs) {
612616
if (data.type === 'group') {
613-
this._collectChildrenKey(data.children, key, list);
617+
this._collectChildrenKey(data.children, keys, list);
614618
continue;
615619
}
616-
list.push(data[key]);
620+
keys.forEach(item => {
621+
if (!(item in data)) {
622+
return;
623+
}
624+
const value = data[item];
625+
if (value instanceof Array) {
626+
list.push(...value);
627+
return;
628+
}
629+
list.push(value);
630+
});
617631
}
618632
return list;
619633
}

test/resources/WebMapV3.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,8 @@ var mapstudioWebMap_symbol = JSON.stringify({
533533
'text-max-width': 10,
534534
'icon-anchor': 'center',
535535
'text-ignore-placement': false,
536-
'icon-rotate': 0
536+
'icon-rotate': 0,
537+
'text-z-offset': 0
537538
},
538539
metadata: {},
539540
paint: {
@@ -547,8 +548,7 @@ var mapstudioWebMap_symbol = JSON.stringify({
547548
'text-halo-width': 1,
548549
'icon-opacity': 0.9,
549550
'text-opacity': 1,
550-
'text-translate': [0, 0],
551-
'text-z-offset': 0
551+
'text-translate': [0, 0]
552552
},
553553
id: 'ms_站点3_1712735857741_21',
554554
source: 'ms_1755873792_1712735857741_22',

0 commit comments

Comments
 (0)