Skip to content

Commit 7376e8e

Browse files
committed
【feature】mapboxgl 重写removeSource isSourceLoaded方法
1 parent 1532d42 commit 7376e8e

File tree

7 files changed

+62
-7
lines changed

7 files changed

+62
-7
lines changed

src/mapboxgl/core/MapExtend.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,37 @@ export var MapExtend = (function () {
3333

3434
getSource(sourceId) {
3535
const customOverlayerLayer = Object.values(this.overlayLayersManager).find(item => item.sourceId === sourceId);
36-
if (customOverlayerLayer && customOverlayerLayer.getSource) {
37-
return customOverlayerLayer.getSource();
36+
if (customOverlayerLayer) {
37+
if (customOverlayerLayer.getSource) {
38+
return customOverlayerLayer.getSource();
39+
}
40+
return;
3841
}
3942
return originMapProto.getSource.call(this, sourceId);
4043
}
4144

45+
removeSource(sourceId) {
46+
const customOverlayerLayer = Object.values(this.overlayLayersManager).find(item => item.sourceId === sourceId);
47+
if (customOverlayerLayer) {
48+
if (customOverlayerLayer.removeSource) {
49+
return customOverlayerLayer.removeSource();
50+
}
51+
return;
52+
}
53+
return originMapProto.removeSource.call(this, sourceId);
54+
}
55+
56+
isSourceLoaded(sourceId) {
57+
const customOverlayerLayer = Object.values(this.overlayLayersManager).find(item => item.sourceId === sourceId);
58+
if (customOverlayerLayer) {
59+
if (customOverlayerLayer.isSourceLoaded) {
60+
return customOverlayerLayer.isSourceLoaded();
61+
}
62+
return;
63+
}
64+
return originMapProto.isSourceLoaded.call(this, sourceId);
65+
}
66+
4267
getLayer(id) {
4368
const overlayLayer = this.overlayLayersManager[id];
4469
if (overlayLayer) {

src/mapboxgl/overlay/Base.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ export class CustomOverlayLayer {
1919
// 获取 layer source的信息,如 id,type 等
2020
getSource() {}
2121

22+
// 删除 layer source
23+
removeSource() {}
24+
25+
// 返回指定source 是否加载完成
26+
isSourceLoaded() {
27+
return true;
28+
}
29+
2230
// 返回指定样式图层中绘制属性的值
2331
getPaintProperty() {}
2432

src/mapboxgl/overlay/L7Layer.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,15 @@ export class L7Layer extends CustomOverlayLayer {
212212
return sourceInfo;
213213
}
214214

215+
isSourceLoaded() {
216+
if (!this.l7layer) {
217+
return;
218+
}
219+
const layerSource = this.l7layer.layerSource;
220+
return !layerSource.tileset ? true : layerSource.tileset.isLoaded;
221+
}
222+
223+
215224
onAdd(map) {
216225
this.map = map;
217226
if (!map.$l7scene) {

test/mapboxgl/core/MapExtendSpec.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ describe('MapExtend mapboxgl', () => {
328328
sources: {
329329
'raster-tiles': {
330330
type: 'raster',
331-
tiles: [GlobeParameter.ChinaURL + '/zxyTileImage.png?z={z}&x={x}&y={y}'],
331+
tiles: ['base/resources/img/baiduTileTest.png'],
332332
tileSize: 256
333333
}
334334
},
@@ -351,6 +351,8 @@ describe('MapExtend mapboxgl', () => {
351351
getSource: function () {
352352
return {};
353353
},
354+
removeSource: function() {},
355+
isSourceLoaded: function() { return true; },
354356
getLayer: function () {
355357
return {};
356358
},
@@ -382,6 +384,8 @@ describe('MapExtend mapboxgl', () => {
382384
expect(map.getSource('l7_layer_1')).not.toBeUndefined();
383385
expect(map.getSource('raster-tiles')).not.toBeUndefined();
384386
expect(options.getSource.calls.count()).toEqual(1);
387+
expect(map.isSourceLoaded('l7_layer_1')).toBeTruthy();
388+
expect(map.isSourceLoaded('raster-tiles')).toBeTruthy();
385389
expect(map.getLayer('l7_layer_1')).not.toBeUndefined();
386390
expect(map.getLayer('simple-tiles')).not.toBeUndefined();
387391
expect(map.getLayer('heatmap_1')).toEqual(map.overlayLayersManager['heatmap_1']);
@@ -404,6 +408,10 @@ describe('MapExtend mapboxgl', () => {
404408
map.off('click', 'l7_layer_1', cb);
405409
map.off('click', cb);
406410
expect(options.off.calls.count()).toEqual(1);
411+
map.removeSource('l7_layer_1');
412+
map.removeLayer('simple-tiles');
413+
map.removeSource('raster-tiles');
414+
expect(options.removeSource.calls.count()).toEqual(1);
407415
map.remove();
408416
done();
409417
});

test/mapboxgl/mapping/utils/L7LayerUtilSpec.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ describe('L7LayerUtil', () => {
4545
});
4646

4747
it('add od layer', (done) => {
48-
spyOn(FetchRequest, 'get').and.callFake((url, _, options) => {
49-
expect(options.withCredentials).toBe(options.withCredentials);
50-
expect(options.withoutFormatSuffix).toBeTruthy();
48+
spyOn(FetchRequest, 'get').and.callFake((url, _, nextOptions) => {
49+
expect(nextOptions.withCredentials).toBe(options.withCredentials);
50+
expect(nextOptions.withoutFormatSuffix).toBeTruthy();
5151
if (url.indexOf('/sprite') > -1) {
5252
return Promise.resolve(new Response(msSpriteInfo));
5353
}
@@ -143,6 +143,8 @@ describe('L7LayerUtil', () => {
143143
const spy = spyOn(nextOptions.map, 'addLayer').and.callThrough();
144144
addL7Layers(nextOptions).then(() => {
145145
expect(nextOptions.map.addLayer.calls.count()).toEqual(2);
146+
expect(layerMaplist['国内航班数据_100']).toBeTruthy();
147+
expect(layerMaplist['ms_composite_国内航班数据_100']).toBeTruthy();
146148
spy.calls.reset();
147149
done();
148150
});

test/mapboxgl/overlay/L7LayerSpec.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ describe('mapboxgl L7Layer', () => {
116116
map.$l7scene = null;
117117
done();
118118
});
119+
expect(layer.isSourceLoaded()).toBeTruthy();
119120
});
120121

121122
it('PointLayer', (done) => {
@@ -420,6 +421,7 @@ describe('mapboxgl L7Layer', () => {
420421
.shape('circle')
421422
.color('#4cfd47');
422423
map.addLayer(layer);
424+
expect(layer.isSourceLoaded()).toBeFalsy();
423425

424426
map.overlayLayersManager = { [layer.id]: layer };
425427
l7Layer.rawConfig.name = 'empty-test';

test/tool/mock_l7.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ class Layer {
8383
}
8484
}
8585
]
86-
])
86+
]),
87+
isLoaded: false
8788
};
8889
}
8990
return this;

0 commit comments

Comments
 (0)