forked from SuperMap/iClient-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapExtend.js
More file actions
309 lines (289 loc) · 9.29 KB
/
MapExtend.js
File metadata and controls
309 lines (289 loc) · 9.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/* Copyright© 2000 - 2023 SuperMap Software Co.Ltd. All rights reserved.
* This program are made available under the terms of the Apache License, Version 2.0
* which accompanies this distribution and is available at http://www.apache.org/licenses/LICENSE-2.0.html.*/
// import mapboxgl from 'mapbox-gl';
import SymbolHandler from '../overlay/symbol/SymbolHandler';
/**
* @function MapExtend
* @description 扩展了 mapboxgl.Map 对图层相关的操作。
* @private
*/
const mapboxgl = window.mapboxgl;
export var MapExtend = (function () {
/**
* 获取symbol管理
* @param {*} map
* @returns {object}
*/
const getSymbolHandler = (map) => {
if (!mapboxgl.Map.prototype.symbolHandler) {
mapboxgl.Map.prototype.symbolHandler = new SymbolHandler(map);
}
return mapboxgl.Map.prototype.symbolHandler;
}
mapboxgl.Map.prototype.overlayLayersManager = {};
if (mapboxgl.Map.prototype.addLayerBak === undefined) {
mapboxgl.Map.prototype.addLayerBak = mapboxgl.Map.prototype.addLayer;
mapboxgl.Map.prototype.addLayer = function (layer, before) {
if (layer.symbol) {
getSymbolHandler(this).addLayer(layer, before);
return this;
}
if (layer.source || layer.type === 'custom' || layer.type === 'background') {
this.addLayerBak(layer, before);
return this;
}
if (this.overlayLayersManager[layer.id] || this.style._layers[layer.id]) {
this.fire('error', {
error: new Error('A layer with this id already exists.')
});
return;
}
addLayer(layer, this);
this.overlayLayersManager[layer.id] = layer;
return this;
};
}
mapboxgl.Map.prototype.getLayer = function (id) {
if (this.overlayLayersManager[id]) {
return this.overlayLayersManager[id];
}
return this.style.getLayer(id);
};
mapboxgl.Map.prototype.moveLayer = function (id, beforeId) {
if (this.overlayLayersManager[id]) {
this.overlayLayersManager[id].moveLayer
? this.overlayLayersManager[id].moveLayer(id, beforeId)
: moveLayer(id, beforeId);
return this;
}
if (this.style._layers[id]) {
this.style.moveLayer(id, beforeId);
this._update(true);
return this;
}
};
mapboxgl.Map.prototype.removeLayer = function (id) {
if (this.overlayLayersManager[id]) {
removeLayer(this.overlayLayersManager[id]);
delete this.overlayLayersManager[id];
return this;
}
this.style.removeLayer(id);
this._update(true);
return this;
};
//目前扩展的overlayer,只支持显示或隐藏图层操作
mapboxgl.Map.prototype.setLayoutProperty = function (layerID, name, value) {
if (this.overlayLayersManager[layerID]) {
if (name === 'visibility') {
if (value === 'visible') {
value = true;
} else {
value = false;
}
setVisibility(this.overlayLayersManager[layerID], value);
this.style.fire('data', { dataType: 'style' });
}
return this;
}
this.style.setLayoutProperty(layerID, name, value);
this._update(true);
return this;
};
mapboxgl.Map.prototype.updateTransform = function (units, originX, originY, centerX, centerY, width, height) {
this.transform.units = units;
var mercatorZfromAltitude = this.mercatorZfromAltitude;
mapboxgl.MercatorCoordinate.fromLngLat = function (lngLatLike, altitude) {
altitude = altitude || 0;
const lngLat = mapboxgl.LngLat.convert(lngLatLike);
return new mapboxgl.MercatorCoordinate(
(lngLat.lng - originX) / width,
(originY - lngLat.lat) / height,
mercatorZfromAltitude(altitude, lngLat.lat)
);
};
mapboxgl.MercatorCoordinate.prototype.toLngLat = function () {
return new mapboxgl.LngLat(this.x * width + originX, originY - this.y * height);
};
this.customConvertPoint = window.URL.createObjectURL(
new Blob(
[
'customConvertPoint = {projectX:function(x){return (x - ' +
centerX +
') / ' +
width +
' + 0.5},projectY:function(y){y = 0.5 - ((y - ' +
centerY +
') / ' +
height +
');return y < 0 ? 0 : y > 1 ? 1 : y;},toY:function(y){return (0.5-y)*' +
height +
'+' +
centerY +
';}}'
],
{ type: 'text/javascript' }
)
);
};
function addLayer(layer, map) {
layer.onAdd && layer.onAdd(map);
}
/**
* @function MapExtend.prototype.removeFromMap
* @description 移除事件。
*/
function removeLayer(layer) {
layer.removeFromMap && layer.removeFromMap();
}
/**
* @function MapExtend.prototype.setVisibility
* @description 设置图层可见性,设置图层的隐藏,显示,重绘的相应的可见标记。
* @param {boolean} [visibility] - 是否显示图层(当前地图的 resolution 在最大最小 resolution 之间)。
*/
function setVisibility(layer, visibility) {
layer.setVisibility && layer.setVisibility(visibility);
}
/**
* @function MapExtend.prototype.moveTo
* @description 将图层移动到某个图层之前。
* @param {string} layerID -待插入的图层 ID。
* @param {boolean} [beforeLayerID] - 是否将本图层插入到图层 id 为 layerID 的图层之前(如果为 false 则将本图层插入到图层 id 为 layerID 的图层之后)。
*/
function moveLayer(layerID, beforeLayerID) {
var layer = document.getElementById(layerID);
// var beforeLayer;
if (beforeLayerID) {
var beforeLayer = document.getElementById(beforeLayerID);
if (!beforeLayer) {
mapboxgl.Evented.prototype.fire('error', {
error: new Error(`Layer with id "${beforeLayerID}" does not exist on this document.`)
});
}
}
if (layer && beforeLayer) {
beforeLayer.parentNode.insertBefore(layer, beforeLayer);
} else {
//当没有传入beforeLayerID ,则默认将图层移动到最上面
layer.parentNode.appendChild(layer);
}
}
/**
* 指定图层设置符号
* @param {string} layerId
* @param {string} symbol
*/
mapboxgl.Map.prototype.setSymbol = function (layerId, symbol) {
getSymbolHandler(this).setSymbol(layerId, symbol);
};
/**
* Layer新增symbol属性
*/
if (!(mapboxgl.Map.prototype).setStyleBak) {
(mapboxgl.Map.prototype).setStyleBak = mapboxgl.Map.prototype.setStyle;
mapboxgl.Map.prototype.setStyle = function (style, options) {
this.setStyleBak(style, options);
this.style.once('style.load', () => {
const symbolLayers = style.layers.filter(l => l.symbol);
symbolLayers.forEach((l) => {
this.setSymbol(l.id, l.symbol);
});
});
return this;
}
}
/**
* 加载Web符号
* @param {string} id
* @param {function} callback
* @returns {undefined}
*/
mapboxgl.Map.prototype.loadSymbol = async function (id, callback) {
if (typeof id === 'string') {
let symbolInfo = getSymbolHandler(this).getSymbol(id);
if (!symbolInfo) {
if (!mapboxgl.supermap.WebSymbol.basePath) {
callback({
message: 'SymbolUrl of WebSymbol is null.'
});
return;
}
const symbolResult = await mapboxgl.supermap.WebSymbol.getSymbol?.(id);
if (!symbolResult) {
callback({
message: 'This symbol is not exists.'
});
return;
}
const { value, image } = symbolResult;
symbolInfo = value;
getSymbolHandler(this).addSymbolImageToMap(value, image);
}
callback(null, symbolInfo);
} else {
callback({
message: 'Symbol id must be a string.'
});
}
};
/**
* 添加符号
* @param {string} id
* @param {object} symbol
*/
mapboxgl.Map.prototype.addSymbol = function (id, symbol) {
getSymbolHandler(this).addSymbol(id, symbol);
};
/**
* 判断符号是否存在
* @param {string} id
*/
mapboxgl.Map.prototype.hasSymbol = function (id) {
if (!id) {
this.fire('error', {
error: new Error('Missing required symbol id')
});
return false;
}
return !!getSymbolHandler(this).getSymbol(id);
};
/**
* 删除符号
* @param {string} id
* @param {object} symbol
*/
mapboxgl.Map.prototype.removeSymbol = function (id) {
getSymbolHandler(this).removeSymbol(id);
};
/**
* 获取图层
* @param {string} id
*/
if (mapboxgl.Map.prototype.getLayerBak === undefined) {
mapboxgl.Map.prototype.getLayerBak = mapboxgl.Map.prototype.getLayer;
mapboxgl.Map.prototype.getLayer = function (layerId) {
return getSymbolHandler(this).getLayer(layerId);
};
}
/**
* 删除图层
* @param {string} layerId
*/
if (mapboxgl.Map.prototype.removeLayerBak === undefined) {
mapboxgl.Map.prototype.removeLayerBak = mapboxgl.Map.prototype.removeLayer;
mapboxgl.Map.prototype.removeLayer = function (layerId) {
getSymbolHandler(this).removeLayer(layerId);
};
}
/**
* 获取style
*/
if (mapboxgl.Map.prototype.getStyleBak === undefined) {
mapboxgl.Map.prototype.getStyleBak = mapboxgl.Map.prototype.getStyle;
mapboxgl.Map.prototype.getStyle = function () {
return getSymbolHandler(this).getStyle();
};
}
})();
window.mapboxgl = mapboxgl;