@@ -52,12 +52,12 @@ const WORLD_WIDTH = 360;
52
52
export class WebMap extends mapboxgl . Evented {
53
53
constructor (
54
54
id ,
55
- options = { } ,
56
- mapOptions = { } ,
57
- map ,
58
- layerFilter = function ( ) {
59
- return true ;
60
- }
55
+ options = {
56
+ layerFilter : function ( ) {
57
+ return true ;
58
+ }
59
+ } ,
60
+ mapOptions = { style : { version : 8 , sources : { } , layers : [ ] } }
61
61
) {
62
62
super ( ) ;
63
63
this . mapId = id ;
@@ -70,12 +70,15 @@ export class WebMap extends mapboxgl.Evented {
70
70
mapOptions . center = [ 0 , 0 ] ;
71
71
mapOptions . zoom = 0 ;
72
72
}
73
+ if ( options . map ) {
74
+ this . map = options . map ;
75
+ }
73
76
this . options = Object . assign ( { } , options ) ;
77
+ delete this . options . map ;
74
78
this . options . serverUrl = transformServerUrl ( options . server ) ;
75
79
this . options . target = options . target || 'map' ;
76
80
this . options . withCredentials = options . withCredentials || false ;
77
81
this . mapOptions = mapOptions ;
78
- this . layerFilter = layerFilter ;
79
82
this . webMapService = new WebMapService ( id , options ) ;
80
83
this . eventTypes = [
81
84
'getmapinfofailed' ,
@@ -85,16 +88,14 @@ export class WebMap extends mapboxgl.Evented {
85
88
'notsupportbaidumap' ,
86
89
'dataflowfeatureupdated' ,
87
90
'addlayerssucceeded' ,
91
+ 'addlayerchanged' ,
88
92
'getlayersfailed' ,
89
93
'beforeremovemap' ,
90
94
'crsnotsupport'
91
95
] ;
92
96
this . _mapInitializedHandler = this . _mapInitializedHandler . bind ( this ) ;
93
97
this . _addLayersSucceededHandler = this . _addLayersSucceededHandler . bind ( this ) ;
94
- if ( map ) {
95
- this . map = map ;
96
- this . _appendLayers = true ;
97
- }
98
+ this . _addLayerChangedHandler = this . _addLayerChangedHandler . bind ( this ) ;
98
99
this . _initWebMap ( ! this . map ) ;
99
100
}
100
101
@@ -221,46 +222,15 @@ export class WebMap extends mapboxgl.Evented {
221
222
this . _handler && this . _handler . updateOverlayLayer ( layerInfo , features , mergeByField ) ;
222
223
}
223
224
224
- getCacheLayerIds ( ) {
225
- return (
226
- this . _cacheLayerIds ||
227
- this . _cacheCleanLayers . reduce ( ( ids , item ) => {
228
- ids . push ( ...item . renderLayers ) ;
229
- return ids ;
230
- } , [ ] )
231
- ) ;
232
- }
233
-
234
225
copyLayer ( id , layerInfo ) {
235
226
return this . _handler && this . _handler . copyLayer ( id , layerInfo ) ;
236
227
}
237
228
238
- cleanLayers ( ) {
239
- this . _taskID = null ;
240
- const sourceList = [ ] ;
229
+ clean ( removeMap = true ) {
241
230
if ( this . map ) {
242
- for ( const item of this . _cacheCleanLayers ) {
243
- item . renderLayers . forEach ( ( layerId ) => {
244
- if ( this . map . getLayer ( layerId ) ) {
245
- this . map . removeLayer ( layerId ) ;
246
- }
247
- } ) ;
248
- if ( this . map . getSource ( item . renderSource . id ) && ! item . l7Layer ) {
249
- sourceList . push ( item . renderSource . id ) ;
250
- }
251
- }
252
- Array . from ( new Set ( sourceList ) ) . forEach ( ( sourceId ) => {
253
- this . map . removeSource ( sourceId ) ;
254
- } ) ;
255
- }
256
- this . _cacheCleanLayers = [ ] ;
257
- }
258
-
259
- clean ( ) {
260
- if ( this . map ) {
261
- this . fire ( 'beforeremovemap' , { map : this . map } ) ;
231
+ removeMap && this . fire ( 'beforeremovemap' , { map : this . map } ) ;
262
232
if ( this . _handler ) {
263
- this . _handler . clean ( ) ;
233
+ this . _handler . clean ( removeMap ) ;
264
234
this . _handler = null ;
265
235
}
266
236
this . map = null ;
@@ -271,6 +241,29 @@ export class WebMap extends mapboxgl.Evented {
271
241
}
272
242
}
273
243
244
+ cleanLayers ( ) {
245
+ // 清空追加的地图图层以及对应的事件
246
+ if ( ! this . map ) {
247
+ return ;
248
+ }
249
+ const sourceList = [ ] ;
250
+ for ( const item of this . _cacheCleanLayers ) {
251
+ item . renderLayers . forEach ( ( layerId ) => {
252
+ if ( this . map . getLayer ( layerId ) ) {
253
+ this . map . removeLayer ( layerId ) ;
254
+ }
255
+ } ) ;
256
+ if ( this . map . getSource ( item . renderSource . id ) && ! item . l7Layer ) {
257
+ sourceList . push ( item . renderSource . id ) ;
258
+ }
259
+ }
260
+ Array . from ( new Set ( sourceList ) ) . forEach ( ( sourceId ) => {
261
+ this . map . removeSource ( sourceId ) ;
262
+ } ) ;
263
+ this . _cacheCleanLayers = [ ] ;
264
+ this . clean ( false ) ;
265
+ }
266
+
274
267
_initWebMap ( clean = true ) {
275
268
clean && this . clean ( ) ;
276
269
if ( this . webMapInfo ) {
@@ -372,7 +365,7 @@ export class WebMap extends mapboxgl.Evented {
372
365
}
373
366
374
367
_createWebMapV2 ( commonOptions , mapOptions ) {
375
- const webMapHandler = new WebMapV2 ( this . mapId , commonOptions , mapOptions , this . layerFilter ) ;
368
+ const webMapHandler = new WebMapV2 ( this . mapId , commonOptions , mapOptions ) ;
376
369
return webMapHandler ;
377
370
}
378
371
@@ -394,16 +387,14 @@ export class WebMap extends mapboxgl.Evented {
394
387
this . fire ( 'mapinitialized' , { map : this . map } ) ;
395
388
}
396
389
397
- _addLayersSucceededHandler ( { mapparams, layers, cacheLayerIds } ) {
398
- this . mapParams = mapparams ;
399
- this . _cacheCleanLayers = layers ;
400
- this . _cacheLayerIds = cacheLayerIds ;
401
- this . fire ( 'addlayerssucceeded' , {
402
- map : this . map ,
403
- mapparams : this . mapParams ,
404
- layers,
405
- cacheLayerIds
406
- } ) ;
390
+ _addLayersSucceededHandler ( params ) {
391
+ this . mapParams = params . mapparams ;
392
+ this . fire ( 'addlayerssucceeded' , params ) ;
393
+ }
394
+
395
+ _addLayerChangedHandler ( params ) {
396
+ this . _cacheCleanLayers = params . layers ;
397
+ this . fire ( 'addlayerchanged' , params ) ;
407
398
}
408
399
409
400
_getMapInfo ( mapInfo ) {
@@ -415,9 +406,7 @@ export class WebMap extends mapboxgl.Evented {
415
406
const commonOptions = {
416
407
...this . options ,
417
408
iportalServiceProxyUrlPrefix : this . webMapService . iportalServiceProxyUrl ,
418
- serverUrl : this . options . serverUrl ,
419
- withCredentials : this . options . withCredentials ,
420
- target : this . options . target
409
+ serverUrl : this . options . serverUrl
421
410
} ;
422
411
const commonEvents = {
423
412
...this . eventTypes . reduce ( ( events , name ) => {
@@ -427,7 +416,8 @@ export class WebMap extends mapboxgl.Evented {
427
416
return events ;
428
417
} , { } ) ,
429
418
mapinitialized : this . _mapInitializedHandler ,
430
- addlayerssucceeded : this . _addLayersSucceededHandler
419
+ addlayerssucceeded : this . _addLayersSucceededHandler ,
420
+ addlayerchanged : this . _addLayerChangedHandler
431
421
} ;
432
422
const mapOptions = cloneDeep ( this . mapOptions ) ;
433
423
switch ( type ) {
@@ -446,10 +436,11 @@ export class WebMap extends mapboxgl.Evented {
446
436
this . _handler . on ( type , commonEvents [ type ] ) ;
447
437
}
448
438
let _mapInfo = { } ;
439
+ const layerFilter = this . options . layerFilter ;
449
440
if ( mapInfo ) {
450
441
_mapInfo = {
451
442
...mapInfo ,
452
- layers : typeof this . layerFilter === 'function' ? mapInfo . layers . filter ( this . layerFilter ) : mapInfo . layers
443
+ layers : typeof layerFilter === 'function' ? mapInfo . layers . filter ( layerFilter ) : mapInfo . layers
453
444
} ;
454
445
}
455
446
this . _handler . initializeMap ( _mapInfo , this . map ) ;
0 commit comments