@@ -3,7 +3,7 @@ import View from './View';
3
3
import './style' ;
4
4
import { isArray , uniqueId } from 'lodash' ;
5
5
import { getMapboxKey , isPaintKey } from '../utils/StyleSettingUtil' ;
6
- // import '../../../../src/mapboxgl/core/MapExtend';
6
+ import '../../../../src/mapboxgl/core/MapExtend' ;
7
7
8
8
const SET_PROPERTY_RULE = {
9
9
paint : 'setPaintProperty' ,
@@ -20,7 +20,7 @@ const App = () => {
20
20
} , [ ! ! map ] ) ;
21
21
22
22
const onLoadedMap = async ( map : any ) => {
23
- ( window as any ) . mapboxgl . supermap . WebSymbol . symbolUrl = "../../libs/resources/symbols" ;
23
+ ( window as any ) . mapboxgl . supermap . WebSymbol . basePath = "../../libs/resources/symbols" ;
24
24
setMap ( map ) ;
25
25
}
26
26
@@ -29,8 +29,8 @@ const App = () => {
29
29
const { symbolId, style = { } } = preSymbolInfo ;
30
30
const id = uniqueId ( ) ;
31
31
await map . loadSymbol ( symbolId , ( _err , symbol ) => {
32
- style . paint && Object . assign ( symbol . paint , style . paint ) ;
33
- style . layout && Object . assign ( symbol . layout , style . layout ) ;
32
+ style . paint && Object . assign ( symbol . paint , style . paint ) ;
33
+ style . layout && Object . assign ( symbol . layout , style . layout ) ;
34
34
map . addSymbol ( id , symbol ) ;
35
35
} ) ;
36
36
return { id, symbolId } ;
@@ -54,7 +54,7 @@ const App = () => {
54
54
symbol : any ;
55
55
layersInfo : any ;
56
56
} ) : void => {
57
- const { layerId, sourceLayer, type, layerType, symbol, layersInfo} = options ;
57
+ const { layerId, sourceLayer, type, layerType, symbol, layersInfo } = options ;
58
58
const { id, symbolId } = symbol ;
59
59
if ( type !== 'text' ) {
60
60
layersInfo . push ( { id : layerId , type, sourceLayer, url, symbolId } ) ;
@@ -145,87 +145,80 @@ const App = () => {
145
145
} ) ;
146
146
147
147
addMVTLayer ( {
148
- layerId :'chinaPolygon' ,
149
- sourceLayer :'China_Province_pg@China' ,
150
- type :'polygon' ,
148
+ layerId : 'chinaPolygon' ,
149
+ sourceLayer : 'China_Province_pg@China' ,
150
+ type : 'polygon' ,
151
151
layerType : 'fill' ,
152
152
symbol : chinaSymbol ,
153
153
layersInfo : newLayersInfo
154
154
} ) ;
155
155
addMVTLayer ( {
156
- layerId :'riverpolygon' ,
156
+ layerId : 'riverpolygon' ,
157
157
sourceLayer : 'Main_River_pg@China' ,
158
- type :'polygon' ,
158
+ type : 'polygon' ,
159
159
layerType : 'fill' ,
160
160
symbol : riverPolygonSymbol ,
161
161
layersInfo : newLayersInfo
162
162
} ) ;
163
163
addMVTLayer ( {
164
- layerId :'riverLine' ,
165
- sourceLayer : 'Main_River_ln@China' ,
166
- type :'line' ,
164
+ layerId : 'riverLine' ,
165
+ sourceLayer : 'Main_River_ln@China' ,
166
+ type : 'line' ,
167
167
layerType : 'line' ,
168
168
symbol : riverLineSymbol ,
169
169
layersInfo : newLayersInfo
170
170
} ) ;
171
171
addMVTLayer ( {
172
- layerId :'provinceLine' ,
172
+ layerId : 'provinceLine' ,
173
173
sourceLayer : 'China_Province_ln@China' ,
174
- type :'line' ,
174
+ type : 'line' ,
175
175
layerType : 'line' ,
176
176
symbol : provinceLineSymbol ,
177
177
layersInfo : newLayersInfo
178
178
} ) ;
179
179
addMVTLayer ( {
180
- layerId :'mainRoadLine' ,
181
- sourceLayer : 'Main_Road_L@China' ,
182
- type :'line' ,
180
+ layerId : 'mainRoadLine' ,
181
+ sourceLayer : 'Main_Road_L@China' ,
182
+ type : 'line' ,
183
183
layerType : 'line' ,
184
184
symbol : MainRoadLineLineSymbol ,
185
185
layersInfo : newLayersInfo
186
186
} ) ;
187
187
addMVTLayer ( {
188
- layerId :'capital' ,
189
- sourceLayer : 'China_Capital_pt@China' ,
190
- type :'point' ,
188
+ layerId : 'capital' ,
189
+ sourceLayer : 'China_Capital_pt@China' ,
190
+ type : 'point' ,
191
191
layerType : 'symbol' ,
192
192
symbol : capitalSymbol ,
193
193
layersInfo : newLayersInfo
194
194
} ) ;
195
195
addMVTLayer ( {
196
- layerId :'city' ,
196
+ layerId : 'city' ,
197
197
sourceLayer : 'China_ProCenCity_pt@China' ,
198
- type :'point' ,
198
+ type : 'point' ,
199
199
layerType : 'symbol' ,
200
200
symbol : citySymbol ,
201
201
layersInfo : newLayersInfo
202
202
} ) ;
203
203
addMVTLayer ( {
204
- layerId :'nationText' ,
204
+ layerId : 'nationText' ,
205
205
sourceLayer : 'China_Nation_B_pt@China' ,
206
- type :'text' ,
206
+ type : 'text' ,
207
207
layerType : 'symbol' ,
208
208
symbol : nationTextSymbol ,
209
209
layersInfo : newLayersInfo
210
210
} ) ;
211
211
addMVTLayer ( {
212
- layerId :'cityText' ,
212
+ layerId : 'cityText' ,
213
213
sourceLayer : 'China_ProCenCity_pt@China' ,
214
- type :'text' ,
214
+ type : 'text' ,
215
215
layerType : 'symbol' ,
216
216
symbol : cityTextSymbol ,
217
217
layersInfo : newLayersInfo
218
218
} ) ;
219
219
setLayersInfo ( newLayersInfo . reverse ( ) ) ;
220
220
} ;
221
221
222
- const hideSymbol = ( symbol ) => {
223
- isArray ( symbol ) ?
224
- symbol . forEach ( s => {
225
- s . layout = Object . assign ( s . layout ?? { } , { visibility : 'none' } ) ;
226
- } ) :
227
- symbol . layout = Object . assign ( symbol . layout ?? { } , { visibility : 'none' } ) ;
228
- } ;
229
222
// 点击切换
230
223
const onIconClick = async ( symbolId , layerId ) => {
231
224
if ( ! map ) return ;
@@ -236,9 +229,6 @@ const App = () => {
236
229
symbol . layout [ 'icon-allow-overlap' ] = true ;
237
230
symbol . layout [ 'icon-size' ] = 0.16 ;
238
231
}
239
- // TODO 暂时这样处理,待symbol是否设置visibility明确后优化
240
- // 图层隐藏状态下切换符号,隐藏新符号
241
- getLayerPropertyStyle ( layerId , 'visibility' ) === 'none' && hideSymbol ( symbol ) ;
242
232
map . addSymbol ( id , symbol ) ;
243
233
} ) ;
244
234
map . setSymbol ( layerId , id ) ;
@@ -253,16 +243,30 @@ const App = () => {
253
243
return map . getStyle ( ) . layers . find ( layer => layer ?. id === layerId ) ;
254
244
} ;
255
245
246
+ /**以下三种方法都有点儿特殊化了 */
247
+ const getCompositeLayerId = ( layerId : string ) : string => {
248
+ return map . symbolHandler . compositeSymbolRender . getLayerId ( layerId ) ;
249
+ } ;
250
+ // const getSymbol = (layerId: string) => {
251
+ // return map.symbolHandler.getSymbol(layerId);
252
+ // }
253
+ const getCompositeLayersIds = ( layerId : string ) : string [ ] => {
254
+ return map . symbolHandler . compositeSymbolRender . getLayerIds ( layerId ) ;
255
+ } ;
256
+ const getImageInfo = ( id : string ) => {
257
+ return map . symbolHandler . symbolManager . getImageInfo ( id ) ;
258
+ }
259
+
256
260
const getLayerPropertyStyle = ( layerId : string , key : string ) => {
257
- const compositeLayerId = map . compositeLayersManager . getCompositeLayerId ( layerId ) ;
261
+ const compositeLayerId = getCompositeLayerId ( layerId ) ;
258
262
const type = getLayerType ( compositeLayerId ?? layerId ) ;
259
263
const layer = getLayer ( layerId ) ;
260
264
const mapboxKey = type && getMapboxKey [ type ] ( key ) ;
261
265
return isPaintKey ( mapboxKey ) ? layer ?. paint ?. [ mapboxKey ] : layer ?. layout ?. [ mapboxKey ] ;
262
266
} ;
263
267
264
268
const changeLayerStyle = ( layerId : string , key : string , value : string ) : void => {
265
- const compositeLayerId = map . compositeLayersManager . getCompositeLayerId ( layerId ) ;
269
+ const compositeLayerId = getCompositeLayerId ( layerId ) ;
266
270
const type = getLayerType ( compositeLayerId ?? layerId ) ;
267
271
const layer = getLayer ( layerId ) ;
268
272
const mapboxKey = type && getMapboxKey [ type ] ( key ) ;
@@ -272,12 +276,12 @@ const App = () => {
272
276
const ImageId : string = layer ?. layout ?. [ 'icon-image' ] ;
273
277
if ( ImageId && ! ImageId . startsWith ( 'sdf_' ) ) {
274
278
const sdfImageId = uniqueId ( 'sdf_' ) ;
275
- const image = map . symbolManager . getImageInfo ( ImageId ) ;
279
+ const image = getImageInfo ( ImageId ) ;
276
280
map . addImage ( sdfImageId , image , { sdf : true } ) ;
277
281
map . setLayoutProperty ( layerId , 'icon-image' , sdfImageId ) ;
278
282
}
279
283
} else if ( type === 'line' && key === 'visibility' ) {
280
- const layerIds = map . compositeLayersManager . getLayers ( layerId ) ?? [ layerId ] ;
284
+ const layerIds = getCompositeLayersIds ( layerId ) ?? [ layerId ] ;
281
285
layerIds . forEach ( ( layerId ) => {
282
286
map [ SET_PROPERTY_RULE [ paintOrLayout ] ] ( layerId , mapboxKey , value ) ;
283
287
} ) ;
@@ -286,28 +290,38 @@ const App = () => {
286
290
map [ SET_PROPERTY_RULE [ paintOrLayout ] ] ( layerId , mapboxKey , value ) ;
287
291
} ;
288
292
289
- const getCompositeLayersIds = ( layerId : string ) : string [ ] => {
290
- return map . compositeLayersManager . getLayers ( layerId ) ;
291
- } ;
292
- const getImageInfo = ( id : string ) => {
293
- return map . symbolManager . getImageInfo ( id ) ;
293
+ // const testLayerId = "chinaPolygon";
294
+ const testLayerId = "mainRoadLine" ;
295
+ const getLayer2 = ( ) => {
296
+ const layer = map . getLayer ( testLayerId ) ;
297
+ console . log ( layer ) ;
294
298
}
295
- return < View
296
- layerListParams = { {
297
- layersInfo,
298
- setLayersInfo,
299
- onIconClick
300
- } }
301
- mapParams = { {
302
- onLoadedMap
303
- } }
304
- styleSettingParams = { {
305
- changeLayerStyle,
306
- getLayerPropertyStyle,
307
- getCompositeLayersIds,
308
- getImageInfo
309
- } }
310
- />
299
+ const removerLayer = ( ) => {
300
+ map . removeLayer ( testLayerId ) ;
301
+ }
302
+ const getStyle = ( ) => {
303
+ console . log ( map . getStyle ( ) ) ;
304
+ }
305
+
306
+ return < div >
307
+ < button onClick = { getLayer2 } > getLayer</ button >
308
+ < button onClick = { removerLayer } > removerLayer</ button >
309
+ < button onClick = { getStyle } > getStyle</ button > < View
310
+ layerListParams = { {
311
+ layersInfo,
312
+ setLayersInfo,
313
+ onIconClick
314
+ } }
315
+ mapParams = { {
316
+ onLoadedMap
317
+ } }
318
+ styleSettingParams = { {
319
+ changeLayerStyle,
320
+ getLayerPropertyStyle,
321
+ getCompositeLayersIds,
322
+ getImageInfo
323
+ } }
324
+ /> </ div >
311
325
}
312
326
313
327
export default App ;
0 commit comments