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