forked from SuperMap/iClient-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapVRenderer.js
More file actions
390 lines (341 loc) · 12.1 KB
/
MapVRenderer.js
File metadata and controls
390 lines (341 loc) · 12.1 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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/* Copyright© 2000 - 2021 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 L from 'leaflet';
import { baiduMapLayer } from 'mapv';
import { getMeterPerMapUnit } from '../../core/Util';
var BaseLayer = baiduMapLayer ? baiduMapLayer.__proto__ : Function;
/**
* @class L.supermap.MapVRenderer
* @classdesc 地图渲染类。
* @category Visualization MapV
* @private
* @extends mapv.BaseLayer
* @param {L.Map} map - 待渲染的地图。
* @param {L.Layer} layer - 待渲染的图层。
* @param {DataSet} dataSet - 待渲染的数据集。
* @param {Object} options - 渲染的参数。
*/
export class MapVRenderer extends BaseLayer {
constructor(map, layer, dataSet, options) {
super(map, dataSet, options);
if (!BaseLayer) {
return;
}
var self = this;
options = options || {};
self.init(options);
self.argCheck(options);
this.canvasLayer = layer;
this.clickEvent = this.clickEvent.bind(this);
this.mousemoveEvent = this.mousemoveEvent.bind(this);
this._moveStartEvent = this.moveStartEvent.bind(this);
this._moveEndEvent = this.moveEndEvent.bind(this);
this._zoomStartEvent = this.zoomStartEvent.bind(this);
this.bindEvent();
}
/**
* @function L.supermap.MapVRenderer.prototype.clickEvent
* @description 点击事件。
* @param {Object} e - 触发对象。
*/
clickEvent(e) {
var offset = this.map.containerPointToLayerPoint([0, 0]);
var devicePixelRatio = this.devicePixelRatio = this.canvasLayer.devicePixelRatio = window.devicePixelRatio || 1 ;
var pixel = e.layerPoint;
super.clickEvent(L.point((pixel.x - offset.x) / devicePixelRatio, (pixel.y - offset.y) / devicePixelRatio), e);
}
/**
* @function L.supermap.MapVRenderer.prototype.mousemoveEvent
* @description 鼠标移动事件。
* @param {Object} e - 触发对象。
*/
mousemoveEvent(e) {
var pixel = e.layerPoint;
super.mousemoveEvent(pixel, e);
}
/**
* @function L.supermap.MapVRenderer.prototype.bindEvent
* @description 绑定鼠标移动和鼠标点击事件。
* @param {Object} e - 触发对象。
*/
bindEvent() {
var map = this.map;
if (this.options.methods) {
if (this.options.methods.click) {
map.on('click', this.clickEvent);
}
if (this.options.methods.mousemove) {
map.on('mousemove', this.mousemoveEvent);
}
}
this.map.on('movestart', this._moveStartEvent);
this.map.on('moveend', this._moveEndEvent);
this.map.on('zoomstart', this._zoomStartEvent);
}
/**
* @function L.supermap.MapVRenderer.prototype.destroy
* @description 释放资源。
*/
destroy() {
this.unbindEvent();
this.clearData();
this.animator && this.animator.stop();
this.animator = null;
this.canvasLayer = null;
}
/**
* @function L.supermap.MapVRenderer.prototype.unbindEvent
* @description 解绑鼠标移动和鼠标滑动触发的事件。
* @param {Object} e - 触发对象。
*/
unbindEvent() {
var map = this.map;
if (this.options.methods) {
if (this.options.methods.click) {
map.off('click', this.clickEvent);
}
if (this.options.methods.mousemove) {
map.off('mousemove', this.mousemoveEvent);
}
}
this.map.off('movestart', this._moveStartEvent);
this.map.off('moveend', this._moveEndEvent);
this.map.off('zoomstart', this._zoomStartEvent);
}
/**
* @function L.supermap.MapVRenderer.prototype.getContext
* @description 获取信息。
*/
getContext() {
return this.canvasLayer.getCanvas().getContext(this.context);
}
/**
* @function L.supermap.MapVRenderer.prototype.addData
* @description 添加数据。
* @param {Object} data - 待添加的数据。
* @param {Object} options - 待添加的数据信息。
*/
addData(data, options) {
var _data = data;
if (data && data.get) {
_data = data.get();
}
this.dataSet.add(_data);
this.update({
options: options
});
}
/**
* @function L.supermap.MapVRenderer.prototype.update
* @description 更新图层。
* @param {Object} opt - 待更新的数据。
* @param {Object} opt.data - mapv数据集。
* @param {Object} opt.options - mapv绘制参数。
*/
update(opt) {
var update = opt || {};
var _data = update.data;
if (_data && _data.get) {
_data = _data.get();
}
if (_data != undefined) {
this.dataSet.set(_data);
}
super.update({
options: update.options
});
}
/**
* @function L.supermap.MapVRenderer.prototype.getData
* @description 获取数据
*/
getData() {
return this.dataSet;
}
/**
* @function L.supermap.MapVRenderer.prototype.removeData
* @description 删除符合过滤条件的数据。
* @param {Function} filter - 过滤条件。条件参数为数据项,返回值为 true,表示删除该元素;否则表示不删除。
*/
removeData(filter) {
if (!this.dataSet) {
return;
}
var newData = this.dataSet.get({
filter: function (data) {
return (filter != null && typeof filter === "function") ? !filter(data) : true;
}
});
this.dataSet.set(newData);
this.update({
options: null
});
}
/**
* @function L.supermap.MapVRenderer.prototype.clearData
* @description 清除数据
*/
clearData() {
this.dataSet && this.dataSet.clear();
this.update({
options: null
});
}
_canvasUpdate(time) {
if (!this.canvasLayer) {
return;
}
var self = this;
var animationOptions = self.options.animation;
var context = this.getContext();
var map = this.map;
if (self.isEnabledTime()) {
if (time === undefined) {
this.clear(context);
return;
}
if (this.context === '2d') {
context.save();
context.globalCompositeOperation = 'destination-out';
context.fillStyle = 'rgba(0, 0, 0, .1)';
context.fillRect(0, 0, context.canvas.width, context.canvas.height);
context.restore();
}
} else {
this.clear(context);
}
if (this.context === '2d') {
for (var key in self.options) {
context[key] = self.options[key];
}
} else {
context.clear(context.COLOR_BUFFER_BIT);
}
if (self.options.minZoom && map.getZoom() < self.options.minZoom || self.options.maxZoom && map.getZoom() > self.options.maxZoom) {
return;
}
var bounds = map.getBounds();
//获取当前像素下的地理范围
var dw = bounds.getEast() - bounds.getWest();
var dh = bounds.getNorth() - bounds.getSouth();
var mapCanvas = map.getSize();
var resolutionX = dw / mapCanvas.x,
resolutionY = dh / mapCanvas.y;
// 一个像素是多少米
var zoomUnit = getMeterPerMapUnit('DEGREE') * resolutionX;
//var centerPx = map.latLngToLayerPoint(map.getCenter());
//获取屏幕左上角的地理坐标坐标
//左上角屏幕坐标为0,0
var topLeft = this.canvasLayer.getTopLeft();
var topLeftPX = map.latLngToAccurateContainerPoint(topLeft);
// var lopLeft = map.containerPointToLatLng([0, 0]);
var dataGetOptions = {
transferCoordinate: function (coordinate) {
var offset;
if (self.context === '2d') {
offset = map.latLngToAccurateContainerPoint(L.latLng(coordinate[1], coordinate[0]));
} else {
offset = {
'x': (coordinate[0] - topLeft.lng) / resolutionX,
'y': (topLeft.lat - coordinate[1]) / resolutionY
};
}
var pixel = {
x: offset.x - topLeftPX.x,
y: offset.y - topLeftPX.y
};
return [pixel.x, pixel.y];
}
};
if (time !== undefined) {
dataGetOptions.filter = function (item) {
var trails = animationOptions.trails || 10;
return (time && item.time > (time - trails) && item.time < time);
}
}
var data = self.dataSet.get(dataGetOptions);
this.processData(data);
var worldPoint = map.latLngToContainerPoint(L.latLng(0, 0));
var pixel = {
x: worldPoint.x - topLeftPX.x,
y: worldPoint.y - topLeftPX.y
};
// 兼容unit为'm'的情况
if (self.options.unit === 'm') {
if (self.options.size) {
self.options._size = self.options.size / zoomUnit;
}
if (self.options.width) {
self.options._width = self.options.width / zoomUnit;
}
if (self.options.height) {
self.options._height = self.options.height / zoomUnit;
}
} else {
self.options._size = self.options.size;
self.options._height = self.options.height;
self.options._width = self.options.width;
}
this.drawContext(context, data, self.options, pixel);
self.options.updateCallback && self.options.updateCallback(time);
}
init(options) {
var self = this;
self.options = options;
this.initDataRange(options);
this.context = self.options.context || '2d';
if (self.options.zIndex) {
this.canvasLayer && this.canvasLayer.setZIndex(self.options.zIndex);
}
this.initAnimator();
}
addAnimatorEvent() {}
/**
* @function L.supermap.MapVRenderer.prototype.moveStartEvent
* @description 开始移动事件。
*/
moveStartEvent() {
var animationOptions = this.options.animation;
if (this.isEnabledTime() && this.animator) {
this.steps.step = animationOptions.stepsRange.start;
this._hide();
}
}
/**
* @function L.supermap.MapVRenderer.prototype.moveEndEvent
* @description 结束移动事件。
*/
moveEndEvent() {
this.canvasLayer.draw();
this._show();
}
/**
* @function L.supermap.MapVRenderer.prototype.zoomStartEvent
* @description 隐藏渲染样式。
*/
zoomStartEvent() {
this._hide();
}
/**
* @function L.supermap.MapVRenderer.prototype.clear
* @description 清除信息。
* @param {string} context - 指定要清除的信息。
*/
clear(context) {
context && context.clearRect && context.clearRect(0, 0, context.canvas.width, context.canvas.height);
}
_hide() {
this.canvasLayer.canvas.style.display = 'none';
}
_show() {
this.canvasLayer.canvas.style.display = 'block';
}
/**
* @function L.supermap.MapVRenderer.prototype.draw
* @description 绘制渲染
*/
draw() {
this.canvasLayer.draw();
}
}