Skip to content

Commit f5769df

Browse files
committed
[feature] map新增getSymbol接口
review by zhaoq
1 parent 31e620d commit f5769df

File tree

5 files changed

+54
-23
lines changed

5 files changed

+54
-23
lines changed

src/mapboxgl/overlay/symbol/MapExtendSymbol.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ function MapExtendSymbol(){
108108
*/
109109
mapboxgl.Map.prototype.loadSymbol = async function (id, callback) {
110110
if (typeof id === 'string') {
111-
let symbolInfo = getSymbolHandler(this).getSymbolInfo(id);
111+
let symbolInfo = this.getSymbol(id);
112112
if (!symbolInfo) {
113113
const symbolResult = await getSymbol(id, this);
114114
if (!symbolResult) {
@@ -138,6 +138,14 @@ function MapExtendSymbol(){
138138
getSymbolHandler(this).addSymbol(id, symbol);
139139
};
140140

141+
/**
142+
* 获取符号信息
143+
* @param {string} id
144+
*/
145+
mapboxgl.Map.prototype.getSymbol = function (id) {
146+
return getSymbolHandler(this).getSymbol(id);
147+
};
148+
141149
/**
142150
* 判断符号是否存在
143151
* @param {string} id
@@ -150,7 +158,7 @@ function MapExtendSymbol(){
150158
return false;
151159
}
152160

153-
return !!getSymbolHandler(this).getSymbolInfo(id);
161+
return !!this.getSymbol(id);
154162
};
155163

156164
/**

src/mapboxgl/overlay/symbol/SymbolHandler.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class SymbolHandler {
3636
if (typeof layer.symbol === 'string') {
3737
const id = layer.symbol;
3838
if (id) {
39-
const symbol = this.symbolManager.getSymbol(id);
39+
const symbol = this.getSymbol(id);
4040
if (!symbol) {
4141
return this.map.fire('error', {
4242
error: new Error(`Symbol "${id}" could not be loaded. Please make sure you have added the symbol with map.addSymbol().`)
@@ -190,7 +190,7 @@ class SymbolHandler {
190190
* @param {object} symbol
191191
*/
192192
addSymbol(id, symbol) {
193-
if (this.symbolManager.getSymbol(id)) {
193+
if (this.getSymbol(id)) {
194194
return this.map.fire('error', {
195195
error: new Error('An symbol with this name already exists.')
196196
});
@@ -218,7 +218,7 @@ class SymbolHandler {
218218
* @param {string} layerId
219219
* @return {string | array} symbol
220220
*/
221-
getSymbol(layerId) {
221+
getLayerSymbol(layerId) {
222222
return this._layerSymbols[layerId];
223223
}
224224

@@ -242,7 +242,7 @@ class SymbolHandler {
242242
* 通过symbolId获取symbol内容
243243
* @param {string} symbolId
244244
*/
245-
getSymbolInfo(symbolId) {
245+
getSymbol(symbolId) {
246246
return this.symbolManager.getSymbol(symbolId);
247247
}
248248

@@ -280,7 +280,7 @@ class SymbolHandler {
280280
*/
281281
getLayer(layerId) {
282282
const layer = this.map.getLayerBySymbolBak(layerId);
283-
const symbol = this.getSymbol(layerId);
283+
const symbol = this.getLayerSymbol(layerId);
284284
if (layer) {
285285
return symbol ? { ...layer, symbol } : layer;
286286
} else {
@@ -316,9 +316,9 @@ class SymbolHandler {
316316
style.layers = style.layers.reduce((pre, layer) => {
317317
const compositeId = this.getLayerId(layer.id);
318318
if (compositeId) {
319-
!pre.find(l => l.id === compositeId) && pre.push({ ...layer, symbol: this.getSymbol(compositeId), id: compositeId })
320-
} else if (this.getSymbol(layer.id)) {
321-
pre.push({ ...layer, symbol: this.getSymbol(layer.id) })
319+
!pre.find(l => l.id === compositeId) && pre.push({ ...layer, symbol: this.getLayerSymbol(compositeId), id: compositeId })
320+
} else if (this.getLayerSymbol(layer.id)) {
321+
pre.push({ ...layer, symbol: this.getLayerSymbol(layer.id) })
322322
} else {
323323
pre.push(layer);
324324
}
@@ -361,12 +361,12 @@ class SymbolHandler {
361361
* @param {object} options
362362
*/
363363
setFilter(layerId, filter, options) {
364-
const symbol = this.getSymbol(layerId);
364+
const symbol = this.getLayerSymbol(layerId);
365365
if (isMapboxExpression(symbol)) {
366366
// 如果 symbol 是数据驱动,filter需要重新计算
367367
const realLayerId = this.getFirstLayerId(layerId);
368368
this.map.style.setFilter(realLayerId, filter, options);
369-
const symbol = this.getSymbol(layerId);
369+
const symbol = this.getLayerSymbol(layerId);
370370
this.setSymbol(layerId, symbol);
371371
return;
372372
}
@@ -479,7 +479,7 @@ class SymbolHandler {
479479
*/
480480
updateSymbol(symbolId, symbol) {
481481
// symbol不存在
482-
if (!this.symbolManager.getSymbol(symbolId)) {
482+
if (!this.getSymbol(symbolId)) {
483483
return this.map.fire('error', {
484484
error: new Error(`Symbol "${symbolId}" could not be loaded. Please make sure you have added the symbol with map.addSymbol().`)
485485
});
@@ -503,7 +503,7 @@ class SymbolHandler {
503503
* @param {any} value
504504
*/
505505
setSymbolProperty(symbolId, symbolIndex, name, value) {
506-
const symbol = this.symbolManager.getSymbol(symbolId);
506+
const symbol = this.getSymbol(symbolId);
507507
// symbol不存在
508508
if (!symbol) {
509509
return this.map.fire('error', {
@@ -547,7 +547,7 @@ class SymbolHandler {
547547
* @returns {any}
548548
*/
549549
getSymbolProperty(symbolId, symbolIndex, name) {
550-
const symbol = this.symbolManager.getSymbol(symbolId);
550+
const symbol = this.getSymbol(symbolId);
551551
// symbol不存在
552552
if (!symbol) {
553553
this.map.fire('error', {

src/mapboxgl/overlay/symbol/WebSymbol.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,19 @@ import MapExtendSymbol from './MapExtendSymbol';
117117
* ```
118118
*
119119
*
120+
* ## mapboxgl.Map.prototype.getSymbol
121+
* 获取指定 ID 的符号信息。
122+
*
123+
* 参数名称 |类型 |描述
124+
* :---- |:--- |:---
125+
* id |string |符号ID
126+
*
127+
* **Example**
128+
* ```
129+
* const point1 = map.getSymbol('point-1');
130+
* ```
131+
*
132+
*
120133
* ## mapboxgl.Map.prototype.setSymbolProperty
121134
* 设置指定ID符号的属性值。
122135
*

test/mapboxgl/overlay/symbol/MapExtendSymbolSpec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,4 +271,14 @@ describe('MapExtendSymbol', () => {
271271
expect(map.symbolHandler.getSymbolProperty).toHaveBeenCalled();
272272
expect(value).toBe(5);
273273
});
274+
it('map.getSymbol', () => {
275+
spyOn(map.symbolHandler, 'getSymbol').and.returnValue({
276+
paint: {
277+
"icon-color": "red"
278+
}
279+
});
280+
const value = map.getSymbol("point-1");
281+
expect(map.symbolHandler.getSymbol).toHaveBeenCalled();
282+
expect(value.paint["icon-color"]).toBe("red");
283+
});
274284
});

test/mapboxgl/overlay/symbol/SymbolHandlerSpec.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ describe('mapboxgl_symbol_SymbolHandler', () => {
8383
// spyOn(handler, 'setSymbolTolayer')
8484
// spyOn(handler.singleSymbolRender, 'addLayer')
8585
// handler.addLayer(layer);
86-
// const result = handler.getSymbol("Landuse_R@Jingjin#line");
86+
// const result = handler.getLayerSymbol("Landuse_R@Jingjin#line");
8787
// expect(result).toEqual("line-962464");
8888
// });
8989
it('addLayer-symbolId-1', () => {
@@ -116,7 +116,7 @@ describe('mapboxgl_symbol_SymbolHandler', () => {
116116
spyOn(handler.symbolManager, 'getSymbol').and.returnValue(null);
117117
spyOn(handler.compositeSymbolRender, 'addLayerId');
118118
handler.addLayer(layer);
119-
const result = handler.getSymbol("Landuse_R@Jingjin#line");
119+
const result = handler.getLayerSymbol("Landuse_R@Jingjin#line");
120120
expect(result).toEqual(expression);
121121
expect(handler.compositeSymbolRender.addLayerId).toHaveBeenCalled();
122122
});
@@ -314,7 +314,7 @@ describe('mapboxgl_symbol_SymbolHandler', () => {
314314
id: 'testRemoveLayerId'
315315
});
316316
const symbol = {'paint': {'line-color': '#0fffff'}};
317-
spyOn(handler, 'getSymbol').and.returnValue(symbol);
317+
spyOn(handler, 'getLayerSymbol').and.returnValue(symbol);
318318
const result = handler.getLayer('testRemoveLayerId');
319319
expect(result.symbol).toEqual(symbol);
320320
});
@@ -323,7 +323,7 @@ describe('mapboxgl_symbol_SymbolHandler', () => {
323323
spyOn(handler.map, 'getLayerBySymbolBak').and.returnValue({
324324
id: 'testRemoveLayerId'
325325
});
326-
spyOn(handler, 'getSymbol').and.returnValue(null);
326+
spyOn(handler, 'getLayerSymbol').and.returnValue(null);
327327
const result = handler.getLayer('testRemoveLayerId');
328328
expect(result.symbol).toBeUndefined();
329329
});
@@ -338,7 +338,7 @@ describe('mapboxgl_symbol_SymbolHandler', () => {
338338
"line-color": "#000000"
339339
}
340340
};
341-
spyOn(handler, 'getSymbol').and.returnValue(symbol);
341+
spyOn(handler, 'getLayerSymbol').and.returnValue(symbol);
342342
spyOn(handler, 'getLayerIds').and.returnValue(['child1', 'child2']);
343343
const result = handler.getLayer('testRemoveLayerId');
344344
expect(result.id).toBe('testRemoveLayerId');
@@ -373,7 +373,7 @@ describe('mapboxgl_symbol_SymbolHandler', () => {
373373
});
374374
spyOn(handler, 'hasSymbol').and.returnValue(true);
375375
spyOn(handler, 'getLayerId').and.returnValue(["child1", "child2"]);
376-
spyOn(handler, 'getSymbol').and.returnValue({
376+
spyOn(handler, 'getLayerSymbol').and.returnValue({
377377
paint: {
378378
'line-color': '#ffffff'
379379
}
@@ -396,7 +396,7 @@ describe('mapboxgl_symbol_SymbolHandler', () => {
396396
});
397397
spyOn(handler, 'hasSymbol').and.returnValue(true);
398398
spyOn(handler, 'getLayerId').and.returnValue(undefined);
399-
spyOn(handler, 'getSymbol').and.returnValue({
399+
spyOn(handler, 'getLayerSymbol').and.returnValue({
400400
paint: {
401401
'line-color': '#ffffff'
402402
}
@@ -419,7 +419,7 @@ describe('mapboxgl_symbol_SymbolHandler', () => {
419419
});
420420
spyOn(handler, 'hasSymbol').and.returnValue(true);
421421
spyOn(handler, 'getLayerId').and.returnValue(undefined);
422-
spyOn(handler, 'getSymbol').and.returnValue(undefined);
422+
spyOn(handler, 'getLayerSymbol').and.returnValue(undefined);
423423
const result = handler.getStyle();
424424
expect(result.layers[0].symbol).toBeUndefined();
425425
});

0 commit comments

Comments
 (0)