Skip to content

Commit 92f9a92

Browse files
committed
【update】when mapId is JSON
1 parent 51fb498 commit 92f9a92

File tree

3 files changed

+47
-52
lines changed

3 files changed

+47
-52
lines changed

src/mapboxgl/mapping/WebMap.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,6 @@ export class WebMap extends mapboxgl.Evented {
5151
this.options.withCredentials = options.withCredentials || false;
5252
this.mapOptions = mapOptions;
5353
this._createWebMap();
54-
this.on('mapinitialized', () => {
55-
this.map.on('remove', () => {
56-
this._stopCanvg();
57-
});
58-
});
5954
}
6055
/**
6156
* @function WebMap.prototype.resize

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

Lines changed: 19 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ export class WebMap extends mapboxgl.Evented {
136136
*/
137137
_createMap() {
138138
let {
139-
name,
139+
name = '',
140140
crs,
141141
center = new mapboxgl.LngLat(0, 0),
142142
zoom = 0,
@@ -188,6 +188,15 @@ export class WebMap extends mapboxgl.Evented {
188188
this.fire('projectionisnotmatch');
189189
return;
190190
}
191+
if (Object.prototype.toString.call(this.mapId) === '[object Object]') {
192+
this.mapParams = {
193+
title: this._mapInfo.name,
194+
description: ''
195+
};
196+
this._createMapRelatedInfo();
197+
this._addLayersToMap();
198+
return;
199+
}
191200
Promise.all([this._getMapRelatedInfo(), this._getSpriteDatas()])
192201
.then(([relatedInfo]) => {
193202
this.mapParams = {
@@ -240,56 +249,21 @@ export class WebMap extends mapboxgl.Evented {
240249
* @description emit 图层加载成功事件。
241250
*/
242251
_addLayersToMap() {
243-
const { sources, layers, layerIdMapList } = this._setUniqueId(this._mapInfo);
252+
const { sources, layers } = this._mapInfo;
244253
layers.forEach((layer) => {
245254
layer.source && !this.map.getSource(layer.source) && this.map.addSource(layer.source, sources[layer.source]);
246255
this.map.addLayer(layer);
247256
});
248-
this._sendMapToUser(layerIdMapList);
249-
}
250-
251-
/**
252-
* @private
253-
* @function WebMap.prototype._setUniqueId
254-
* @description 返回唯一 id 的 sources 和 layers。
255-
* @param {Object} mapInfo - map 信息。
256-
*/
257-
_setUniqueId(mapInfo) {
258-
let layersToMap = JSON.parse(JSON.stringify(mapInfo.layers));
259-
const nextSources = {};
260-
const layerIdToChange = {};
261-
for (let sourceId in mapInfo.sources) {
262-
let timestamp = this.map.getSource(sourceId) ? `_${+new Date()}` : '';
263-
const nextSourceId = sourceId + timestamp;
264-
nextSources[nextSourceId] = mapInfo.sources[sourceId];
265-
layersToMap = layersToMap.map((layer) => {
266-
let nextLayer = layer;
267-
if (layer.source === sourceId) {
268-
let layerId = layer.id;
269-
if (this.map.getLayer(layerId)) {
270-
layerId = timestamp ? layer.id + timestamp : `${layer.id}_${+new Date()}`;
271-
}
272-
nextLayer = Object.assign({}, layer, { id: layerId, source: nextSourceId });
273-
}
274-
layerIdToChange[layer.id] = nextLayer.id;
275-
return nextLayer;
276-
});
277-
}
278-
return {
279-
sources: nextSources,
280-
layers: layersToMap,
281-
layerIdMapList: layerIdToChange
282-
};
257+
this._sendMapToUser();
283258
}
284259

285260
/**
286261
* @private
287262
* @function WebMap.prototype._sendMapToUser
288263
* @description emit 图层加载成功事件。
289-
* @param {Object} layerIdMapList - 图层 id 信息
290264
*/
291-
_sendMapToUser(layerIdMapList) {
292-
this._appreciableLayers = this._generateLayers(layerIdMapList);
265+
_sendMapToUser() {
266+
this._appreciableLayers = this._generateLayers();
293267
this.fire('addlayerssucceeded', { map: this.map, mapparams: this.mapParams, layers: this._appreciableLayers });
294268
}
295269

@@ -314,9 +288,11 @@ export class WebMap extends mapboxgl.Evented {
314288

315289
clean() {
316290
if (this.map) {
291+
this.map.remove();
317292
this.map = null;
318293
this._legendList = [];
319294
this.mapOptions = {};
295+
this.options = {};
320296
this._appreciableLayers = [];
321297
}
322298
}
@@ -325,18 +301,16 @@ export class WebMap extends mapboxgl.Evented {
325301
* @private
326302
* @function WebMap.prototype._generateV2LayersStructure
327303
* @description emit 图层加载成功事件。
328-
* @param {Array<Object>} layers - 图层信息。
329304
*/
330-
_generateLayers(layerIdMapList) {
305+
_generateLayers() {
331306
const { catalogs = [] } = this._mapResourceInfo;
332307
const originLayers = this._getLayerInfosFromCatalogs(catalogs);
333308
const layers = originLayers.map((layer) => {
334309
const { title, visualization } = layer;
335-
const realLayerId = layerIdMapList[layer.id];
336310
const layerFromMapInfo = this._mapInfo.layers.find((item) => {
337311
return item.id === layer.id;
338312
});
339-
this._createLegendInfo(Object.assign({}, layerFromMapInfo, { id: realLayerId }), visualization);
313+
this._createLegendInfo(Object.assign({}, layerFromMapInfo), visualization);
340314
let dataType = '';
341315
let dataId = '';
342316
for (const data of this._mapResourceInfo.datas) {
@@ -352,7 +326,7 @@ export class WebMap extends mapboxgl.Evented {
352326
serverId: dataId,
353327
type: dataType
354328
},
355-
layerID: realLayerId,
329+
layerID: layer.id,
356330
layerType: layerFromMapInfo.type === 'raster' ? 'raster' : 'vector',
357331
type: layerFromMapInfo.type,
358332
name: title

test/mapboxgl/mapping/WebMapV3Spec.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { WebMap } from '../../../src/mapboxgl/mapping/WebMap';
2+
import { WebMap as WebMapV3 } from '../../../src/mapboxgl/mapping/webmap/v3/WebMap';
23
import '../../resources/WebMapV3.js';
34
import { FetchRequest } from '@supermap/iclient-common/util/FetchRequest';
45

@@ -21,8 +22,8 @@ describe('mapboxgl-webmap3.0', () => {
2122
});
2223
afterEach(() => {
2324
if (mapstudioWebmap && mapstudioWebmap.map) {
24-
const webMapV3 = mapstudioWebmap._getWebMapInstance();
25-
webMapV3.clean();
25+
const webMapV3 = mapstudioWebmap._getWebMapInstance ? mapstudioWebmap._getWebMapInstance() : mapstudioWebmap;
26+
webMapV3.clean && webMapV3.clean();
2627
mapstudioWebmap = null;
2728
}
2829
window.document.body.removeChild(testDiv);
@@ -119,4 +120,29 @@ describe('mapboxgl-webmap3.0', () => {
119120
done();
120121
});
121122
});
123+
124+
it('mapId is JSON', (done) => {
125+
spyOn(FetchRequest, 'get').and.callFake((url) => {
126+
if (url.indexOf('/sprite') > -1) {
127+
return Promise.resolve(new Response(msSpriteInfo));
128+
}
129+
return Promise.resolve();
130+
});
131+
const mapInfo = JSON.parse(mapstudioWebMap_symbol);
132+
mapstudioWebmap = new WebMapV3(mapInfo, {
133+
server: server,
134+
target: 'map'
135+
});
136+
mapstudioWebmap.initializeMap(mapInfo);
137+
138+
mapstudioWebmap.on('addlayerssucceeded', ({ map }) => {
139+
expect(map).not.toBeUndefined();
140+
expect(mapstudioWebmap.map).toEqual(map);
141+
var style = map.getStyle();
142+
expect(style.layers.length).toBe(mapInfo.layers.length);
143+
expect(mapstudioWebmap.getLayers().length).toBeLessThanOrEqual(mapInfo.layers.length);
144+
expect(mapstudioWebmap.getLegendInfo().length).toBe(0);
145+
done();
146+
});
147+
});
122148
});

0 commit comments

Comments
 (0)