Skip to content

Commit 0c33df1

Browse files
committed
[update] Mapboxgl。Map新增三个接口
updateSymbol、setSymbolProperty、getSymbolProperty review by zhaoq
1 parent af1c643 commit 0c33df1

File tree

6 files changed

+332
-13
lines changed

6 files changed

+332
-13
lines changed

src/mapboxgl/overlay/symbol/MapExtendSymbol.js

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,41 @@ function MapExtendSymbol(){
156156
/**
157157
* 删除符号
158158
* @param {string} id
159-
* @param {object} symbol
160159
*/
161160
mapboxgl.Map.prototype.removeSymbol = function (id) {
162161
getSymbolHandler(this).removeSymbol(id);
163162
};
163+
164+
/**
165+
* 更新符号
166+
* @param {string} id
167+
* @param {object} symbol
168+
*/
169+
mapboxgl.Map.prototype.updateSymbol = function (id, symbol) {
170+
getSymbolHandler(this).updateSymbol(id, symbol);
171+
};
172+
173+
/**
174+
* 设置symbol属性值
175+
* @param {string} id
176+
* @param {number} index
177+
* @param {string} name
178+
* @param {any} value
179+
*/
180+
mapboxgl.Map.prototype.setSymbolProperty = function (id, index, name, value) {
181+
getSymbolHandler(this).setSymbolProperty(id, index, name, value);
182+
};
183+
184+
/**
185+
* 获取symbol的属性值
186+
* @param {string} id
187+
* @param {number} index
188+
* @param {string} name
189+
* @returns {any}
190+
*/
191+
mapboxgl.Map.prototype.getSymbolProperty = function (id, index, name) {
192+
return getSymbolHandler(this).getSymbolProperty(id, index, name);
193+
};
164194

165195
mapboxgl.Map.prototype.getStyle = function () {
166196
if (this.style) {
@@ -175,14 +205,14 @@ function MapExtendSymbol(){
175205
}
176206
getSymbolHandler(this).setFilter(layerId, filter, options);
177207
return this._update(true);
178-
}
208+
};
179209

180210
mapboxgl.Map.prototype.getFilter = function (layerId) {
181211
if (this.style.getLayer(layerId)) {
182212
return this.style.getFilter(layerId);
183213
}
184214
return getSymbolHandler(this).getFilter(layerId);
185-
}
215+
};
186216

187217
mapboxgl.Map.prototype.setLayerZoomRange = function (layerId, minzoom, maxzoom) {
188218
if (this.style.getLayer(layerId)) {
@@ -191,7 +221,7 @@ function MapExtendSymbol(){
191221
}
192222
getSymbolHandler(this).setLayerZoomRange(layerId, minzoom, maxzoom);
193223
return this._update(true);
194-
}
224+
};
195225

196226
mapboxgl.Map.prototype.setPaintProperty = function (layerId, name, value, options) {
197227
if (this.style.getLayer(layerId)) {
@@ -200,22 +230,21 @@ function MapExtendSymbol(){
200230
}
201231
getSymbolHandler(this).setPaintProperty(layerId, name, value, options);
202232
return this._update(true);
203-
}
233+
};
204234

205235
mapboxgl.Map.prototype.getPaintProperty = function (layerId, name) {
206236
if (this.style.getLayer(layerId)) {
207237
return this.style.getPaintProperty(layerId, name);
208238
}
209239
return getSymbolHandler(this).getPaintProperty(layerId, name);
210-
}
240+
};
211241

212242
mapboxgl.Map.prototype.getLayoutProperty = function (layerId, name) {
213243
if (this.style.getLayer(layerId)) {
214244
return this.style.getLayoutProperty(layerId, name);
215245
}
216246
return getSymbolHandler(this).getLayoutProperty(layerId, name);
217-
}
218-
247+
};
219248

220249
/**
221250
* @function WebSymbol.prototype.getSymbol

src/mapboxgl/overlay/symbol/SymbolHandler.js

Lines changed: 106 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Util } from "@supermap/iclient-common/commontypes/Util";
55
import CompositeSymbolRender from "./CompositeSymbolRender";
66
import SingleSymbolRender from "./SingleSymbolRender";
77
import SymbolManager from "./SymbolManager";
8-
import { getImageKey, isMapboxExpression, isMultiSymbol, validateSymbol } from "./SymbolUtil";
8+
import { getImageKey, isMapboxExpression, isMultiSymbol, isPaintKey, validateSymbol } from "./SymbolUtil";
99

1010
/**
1111
* 符号图层管理器
@@ -385,7 +385,7 @@ class SymbolHandler {
385385
*/
386386
getFilter(layerId) {
387387
const realLayerId = this.getFirstLayerId(layerId);
388-
if(this.map.style.getLayer(realLayerId)) {
388+
if (this.map.style.getLayer(realLayerId)) {
389389
return this.map.style.getFilter(realLayerId);
390390
}
391391
}
@@ -458,6 +458,110 @@ class SymbolHandler {
458458
const realLayerId = this.getFirstLayerId(layerId);
459459
return this.map.style.getLayoutProperty(realLayerId, name);
460460
}
461+
462+
/**
463+
* 遍历this._layerSymbols, 更新使用到symbolId的图层
464+
* @param {string} symbolId
465+
*/
466+
updateLayerSymbol(symbolId) {
467+
Object.keys(this._layerSymbols).forEach(layerId => {
468+
const symbol = this._layerSymbols[layerId];
469+
if (symbol.includes(symbolId)) {
470+
this.setSymbol(layerId, symbol);
471+
}
472+
})
473+
}
474+
475+
/**
476+
* 更新符号
477+
* @param {string} symbolId
478+
* @param {object | array} symbol
479+
*/
480+
updateSymbol(symbolId, symbol) {
481+
// symbol不存在
482+
if (!this.symbolManager.getSymbol(symbolId)) {
483+
return this.map.fire('error', {
484+
error: new Error(`Symbol "${symbolId}" could not be loaded. Please make sure you have added the symbol with map.addSymbol().`)
485+
});
486+
}
487+
if (validateSymbol(symbol)) {
488+
// 更新symbol
489+
this.symbolManager.addSymbol(symbolId, symbol);
490+
this.updateLayerSymbol(symbolId);
491+
} else {
492+
return this.map.fire('error', {
493+
error: new Error('Symbol is not supported expressions.')
494+
});
495+
}
496+
}
497+
498+
/**
499+
* 设置symbol属性值
500+
* @param {string} symbolId
501+
* @param {number} symbolIndex
502+
* @param {string} name
503+
* @param {any} value
504+
*/
505+
setSymbolProperty(symbolId, symbolIndex, name, value) {
506+
const symbol = this.symbolManager.getSymbol(symbolId);
507+
// symbol不存在
508+
if (!symbol) {
509+
return this.map.fire('error', {
510+
error: new Error(`Symbol "${symbolId}" could not be loaded. Please make sure you have added the symbol with map.addSymbol().`)
511+
});
512+
}
513+
// value不支持表达式
514+
if (isMapboxExpression(value)) {
515+
return this.map.fire('error', {
516+
error: new Error('Symbol value is not supported expressions.')
517+
});
518+
}
519+
const paintOrLayout = isPaintKey(name) ? 'paint' : 'layout';
520+
if (symbol.length > 0) {
521+
const symbolChild = symbol[symbolIndex];
522+
if (!symbolChild) {
523+
return this.map.fire('error', {
524+
error: new Error(`symbol[${symbolIndex}] does not exist.`)
525+
});
526+
}
527+
if (!symbolChild[paintOrLayout]) {
528+
symbolChild[paintOrLayout] = {};
529+
}
530+
Object.assign(symbolChild[paintOrLayout], { [name]: value });
531+
} else {
532+
if (!symbol[paintOrLayout]) {
533+
symbol[paintOrLayout] = {};
534+
}
535+
Object.assign(symbol[paintOrLayout], { [name]: value });
536+
}
537+
// 更新symbol
538+
this.symbolManager.addSymbol(symbolId, symbol);
539+
this.updateLayerSymbol(symbolId);
540+
}
541+
542+
/**
543+
* 获取symbol的属性值
544+
* @param {string} symbolId
545+
* @param {number} symbolIndex
546+
* @param {string} name
547+
* @returns {any}
548+
*/
549+
getSymbolProperty(symbolId, symbolIndex, name) {
550+
const symbol = this.symbolManager.getSymbol(symbolId);
551+
// symbol不存在
552+
if (!symbol) {
553+
this.map.fire('error', {
554+
error: new Error(`Symbol "${symbolId}" could not be loaded. Please make sure you have added the symbol with map.addSymbol().`)
555+
});
556+
return;
557+
}
558+
const paintOrLayout = isPaintKey(name) ? 'paint' : 'layout';
559+
if (symbol.length > 0) {
560+
return symbol[symbolIndex] && symbol[symbolIndex][paintOrLayout] && symbol[symbolIndex][paintOrLayout][name];
561+
} else {
562+
return symbol[paintOrLayout] && symbol[paintOrLayout][name];
563+
}
564+
}
461565
}
462566

463567
export default SymbolHandler;

src/mapboxgl/overlay/symbol/SymbolUtil.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,4 +173,39 @@ export function validateSymbol(symbol) {
173173
return symbolInfo.every((s) => {
174174
return validateStyleKey(s.paint || {}) && validateStyleKey(s.layout || {});
175175
});
176+
}
177+
178+
export const isPaintKey = (key) => {
179+
return [
180+
'icon-color',
181+
'icon-opacity',
182+
'icon-translate',
183+
'line-opacity',
184+
'line-blur',
185+
'line-translate',
186+
'line-color',
187+
'line-width',
188+
'line-offset',
189+
'line-dasharray',
190+
'line-pattern',
191+
'fill-color',
192+
'fill-opacity',
193+
'fill-pattern',
194+
'fill-outline-color',
195+
'text-color',
196+
'text-halo-blur',
197+
'text-halo-color',
198+
'text-halo-width',
199+
'text-opacity',
200+
'text-translate',
201+
'text-translate-anchor',
202+
'circle-blur',
203+
'circle-color',
204+
'circle-opacity',
205+
'circle-translate',
206+
'circle-radius',
207+
'circle-stroke-color',
208+
'circle-stroke-opacity',
209+
'circle-stroke-width'
210+
].includes(key);
176211
}

src/mapboxgl/overlay/symbol/WebSymbol.js

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ import MapExtendSymbol from './MapExtendSymbol';
7575
*
7676
*
7777
* ## mapboxgl.Map.prototype.hasSymbol
78-
* 检查是否存在特定 ID 的符号。
78+
* 检查是否存在指定 ID 的符号。
7979
*
8080
* 参数名称 |类型 |描述
8181
* :---- |:--- |:---
@@ -88,7 +88,7 @@ import MapExtendSymbol from './MapExtendSymbol';
8888
*
8989
*
9090
* ## mapboxgl.Map.prototype.removeSymbol
91-
* 删除特定 ID 的符号。
91+
* 删除指定 ID 的符号。
9292
*
9393
* 参数名称 |类型 |描述
9494
* :---- |:--- |:---
@@ -100,6 +100,56 @@ import MapExtendSymbol from './MapExtendSymbol';
100100
* ```
101101
*
102102
*
103+
* ## mapboxgl.Map.prototype.updateSymbol
104+
* 更新指定 ID 的符号。
105+
*
106+
* 参数名称 |类型 |描述
107+
* :---- |:--- |:---
108+
* id |string |已经添加的符号ID
109+
* |symbol |object |由Mapbox Layers中的[paint](https://docs.mapbox.com/mapbox-gl-js/style-spec/layers/#paint-property)、[layout](https://docs.mapbox.com/mapbox-gl-js/style-spec/layers/#layout-property)(visibility 属性除外)组成的符号对象|||
110+
* | | |参数名称 |类型 |描述 |
111+
* | | |paint |object |Mapbox Layers [paint](https://docs.mapbox.com/mapbox-gl-js/style-spec/layers/#paint-property)|
112+
* | | |layout |object |Mapbox Layers [layout](https://docs.mapbox.com/mapbox-gl-js/style-spec/layers/#layout-property)(visibility 属性除外)|
113+
*
114+
* **Example**
115+
* ```
116+
* map.updateSymbol('point-1', symbol);
117+
* ```
118+
*
119+
*
120+
* ## mapboxgl.Map.prototype.setSymbolProperty
121+
* 设置指定ID符号的属性值。
122+
*
123+
* 参数名称 |类型 |描述
124+
* :---- |:--- |:---
125+
* id |string |符号ID
126+
* index |number |符号数组的index, 符号不是数组的设置为null
127+
* name |string |属性名称
128+
* value |any |属性值
129+
*
130+
* **Example**
131+
* ```
132+
* map.setSymbolProperty('point-1', null, "icon-color", "black");
133+
* map.setSymbolProperty('line-962529', 0, "line-width", 10);
134+
* ```
135+
*
136+
*
137+
* ## mapboxgl.Map.prototype.getSymbolProperty
138+
* 获取指定ID符号的属性值。
139+
*
140+
* 参数名称 |类型 |描述
141+
* :---- |:--- |:---
142+
* id |string |符号ID
143+
* index |number |符号数组的index, 符号不是数组的设置为null
144+
* name |string |属性名称
145+
*
146+
* **Example**
147+
* ```
148+
* map.getSymbolProperty('point-1', null, "icon-color");
149+
* map.getSymbolProperty('line-962529', 0, "line-width");
150+
* ```
151+
*
152+
*
103153
* ## 扩展 [Mapbox Layers](https://docs.mapbox.com/mapbox-gl-js/style-spec/layers/)
104154
* 在[Mapbox Layers](https://docs.mapbox.com/mapbox-gl-js/style-spec/layers/) 属性的基础上新增了symbol 属性, 指定符号ID 或者 [符号表达式](#expression)。
105155
*

test/mapboxgl/overlay/symbol/MapExtendSymbolSpec.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,5 +251,24 @@ describe('MapExtendSymbol', () => {
251251
expect(map.style.setPaintProperty).toHaveBeenCalled();
252252
expect(map.style.getPaintProperty).toHaveBeenCalled();
253253
});
254-
254+
it('map.updateSymbol', () => {
255+
spyOn(map.symbolHandler, 'updateSymbol');
256+
map.updateSymbol("line-1", {
257+
paint: {
258+
"line-width": 10
259+
}
260+
});
261+
expect(map.symbolHandler.updateSymbol).toHaveBeenCalled();
262+
});
263+
it('map.setSymbolProperty', () => {
264+
spyOn(map.symbolHandler, 'setSymbolProperty');
265+
map.setSymbolProperty("line-1", 0, "line-width", 5);
266+
expect(map.symbolHandler.setSymbolProperty).toHaveBeenCalled();
267+
});
268+
it('map.getSymbolProperty', () => {
269+
spyOn(map.symbolHandler, 'getSymbolProperty').and.returnValue(5);
270+
const value = map.getSymbolProperty("line-1", 0, "line-width");
271+
expect(map.symbolHandler.getSymbolProperty).toHaveBeenCalled();
272+
expect(value).toBe(5);
273+
});
255274
});

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy