forked from SuperMap/iClient-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapvLayer.js
More file actions
367 lines (334 loc) · 12.3 KB
/
MapvLayer.js
File metadata and controls
367 lines (334 loc) · 12.3 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
/* Copyright© 2000 - 2018 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 ol from 'openlayers';
import {MapvCanvasLayer} from './MapvCanvasLayer';
import {baiduMapLayer} from "mapv";
var BaiduMapLayer = baiduMapLayer ? baiduMapLayer.__proto__ : Function;
/**
* @class MapvLayer
* @classdesc MapV 图层类。
* @private
* @param {Object} map - 地图。
* @param {Mapv.DataSet} [dataSet] - 数据集。
* @param {Object} options - 参数。
* @param {number} mapWidth - 地图宽度。
* @param {number} mapHeight - 地图高度。
* @param {Object} source - 资源。
* @param {number} options.width - 画布宽。
* @param {number} options.height - 画布高。
* @param {string} [options.paneName='mapPane'] - 窗口名。
* @param {string} [options.context] - 内容。
* @param {number} [options.zIndex] - 层级。
* @param {string} [options.mixBlendMode] - 最小混合模式。
* @extends {Mapv.BaiduMapLayer}
*/
export class MapvLayer extends BaiduMapLayer {
constructor(map, dataSet, options, mapWidth, mapHeight, source) {
super(map, dataSet, options);
this.dataSet = dataSet;
this.mapWidth = mapWidth;
this.mapHeight = mapHeight;
var self = this;
options = options || {};
this.source = source;
self.animator = null;
self.map = map;
self.init(options);
self.argCheck(options);
this.canvasLayer = new MapvCanvasLayer({
map: map,
context: this.context,
paneName: options.paneName,
mixBlendMode: options.mixBlendMode,
enableMassClear: options.enableMassClear,
zIndex: options.zIndex,
width: mapWidth,
height: mapHeight,
update: function update() {
self._canvasUpdate();
}
});
this.clickEvent = this.clickEvent.bind(this);
this.mousemoveEvent = this.mousemoveEvent.bind(this);
map.on('movestart', this.moveStartEvent.bind(this));
map.on('moveend', this.moveEndEvent.bind(this));
map.getView().on('change:center', this.zoomEvent.bind(this));
map.getView().on('change:size', this.sizeEvent.bind(this));
map.on('pointerdrag', this.dragEvent.bind(this));
this.bindEvent();
}
/**
* @function MapvLayer.prototype.init
* @param {Object} options - 参数。
* @description 初始化参数。
*/
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();
}
/**
* @function MapvLayer.prototype.clickEvent
* @param {Object} e - 事件参数。
* @description 点击事件。
*/
clickEvent(e) {
var pixel = e.pixel;
super.clickEvent({x: pixel[0] + this.offset[0], y: pixel[1] + this.offset[1]}, e);
}
/**
* @function MapvLayer.prototype.mousemoveEvent
* @param {Object} e - 事件参数。
* @description 鼠标移动事件。
*/
mousemoveEvent(e) {
var pixel = e.pixel;
super.mousemoveEvent({x: pixel[0], y: pixel[1]}, e);
}
/**
* @function MapvLayer.prototype.dragEvent
* @description 鼠标拖动事件。
*/
dragEvent() {
this.clear(this.getContext());
}
/**
* @function MapvLayer.prototype.zoomEvent
* @description 缩放事件。
*/
zoomEvent() {
this.clear(this.getContext());
}
/**
* @function MapvLayer.prototype.sizeEvent
* @description 地图窗口大小发生变化时触发。
*/
sizeEvent() {
this.canvasLayer.resize();
}
/**
* @function MapvLayer.prototype.moveStartEvent
* @description 开始移动事件。
*/
moveStartEvent() {
var animationOptions = this.options.animation;
if (this.isEnabledTime() && this.animator) {
this.steps.step = animationOptions.stepsRange.start;
}
}
/**
* @function MapvLayer.prototype.moveEndEvent
* @description 结束移动事件。
*/
moveEndEvent() {
this.canvasLayer.draw();
}
/**
* @function MapvLayer.prototype.bindEvent
* @description 绑定事件。
*/
bindEvent() {
var me = this;
var map = me.map;
if (me.options.methods) {
if (me.options.methods.click) {
map.on('click', me.clickEvent);
}
if (me.options.methods.mousemove) {
me.pointerInteraction = new ol.interaction.Pointer();
me.pointerInteraction.handleMoveEvent_ = function (event) {
me.mousemoveEvent(event);
};
map.addInteraction(me.pointerInteraction);
}
}
}
/**
* @function MapvLayer.prototype.unbindEvent
* @description 解除绑定事件。
*/
unbindEvent() {
var map = this.map;
if (this.options.methods) {
if (this.options.methods.click) {
map.un('click', this.clickEvent);
}
if (this.options.methods.mousemove) {
map.removeInteraction(this.pointerInteraction)
}
}
}
/**
* @function MapvLayer.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 MapvLayer.prototype.update
* @description 更新图层。
* @param {Object} opt - 待更新的数据。
* @param {Object} opt.data - 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});
}
draw() {
this.canvasLayer.draw();
}
/**
* @function MapvLayer.prototype.getData
* @description 获取数据。
*/
getData() {
return this.dataSet;
}
/**
* @function MapvLayer.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 MapvLayer.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 map = self.map;
var context = self.canvasLayer.canvas.getContext(self.context);
if (self.isEnabledTime()) {
if (time === undefined) {
self.clear(context);
return;
}
if (self.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 (self.context == '2d') {
for (var key in self.options) {
context[key] = self.options[key];
}
} else {
context.clear(context.COLOR_BUFFER_BIT);
}
var ext = map.getView().calculateExtent();
var topLeftPx = map.getPixelFromCoordinate([ext[0], ext[3]]);
self._mapCenter = map.getView().getCenter();
self._mapCenterPx = map.getPixelFromCoordinate(self._mapCenter);
self._reselutions = map.getView().getResolution();
self._rotation = -map.getView().getRotation();
var dataGetOptions = {
transferCoordinate: function (coordinate) {
var x = (coordinate[0] - self._mapCenter[0]) / self._reselutions,
y = (self._mapCenter[1] - coordinate[1]) / self._reselutions;
var scaledP = [x + self._mapCenterPx[0], y + self._mapCenterPx[1]];
scaledP = scale(scaledP, self._mapCenterPx,1);
/*//有旋转量的时候处理旋转
if (self._rotation !== 0) {
var rotatedP = rotate(scaledP, self._rotation, self._mapCenterPx);
return [rotatedP[0] + self.offset[0], rotatedP[1] + self.offset[1]];
}
//处理放大或缩小级别*/
return [scaledP[0] + self.offset[0], scaledP[1] + self.offset[1]];
}
};
// //获取某像素坐标点pixelP绕中心center逆时针旋转rotation弧度后的像素点坐标。
// function rotate(pixelP, rotation, center) {
// var x = Math.cos(rotation) * (pixelP[0] - center[0]) - Math.sin(rotation) * (pixelP[1] - center[1]) + center[0];
// var y = Math.sin(rotation) * (pixelP[0] - center[0]) + Math.cos(rotation) * (pixelP[1] - center[1]) + center[1];
// return [x, y];
// }
//获取某像素坐标点pixelP相对于中心center进行缩放scaleRatio倍后的像素点坐标。
function scale(pixelP, center, scaleRatio) {
var x = (pixelP[0] - center[0]) * scaleRatio + center[0];
var y = (pixelP[1] - center[1]) * scaleRatio + center[1];
return [x, y];
}
if (time !== undefined) {
dataGetOptions.filter = function (item) {
var trails = animationOptions.trails || 10;
return (time && item.time > (time - trails) && item.time < time);
};
}
if (self.isEnabledTime() && !self.notFirst) {
self.canvasLayer.resize(self.mapWidth, self.mapHeight);
self.notFirst = true;
}
var data = self.dataSet.get(dataGetOptions);
self.processData(data);
self.options._size = self.options.size;
var pixel = map.getPixelFromCoordinate([0, 0]);
pixel = [pixel[0] - topLeftPx[0], pixel[1] - topLeftPx[1]];
this.drawContext(context, data, self.options, {x: pixel[0], y: pixel[1]});
if (self.isEnabledTime()) {
this.source.changed();
}
self.options.updateCallback && self.options.updateCallback(time);
}
isEnabledTime() {
var animationOptions = this.options.animation;
return animationOptions && !(animationOptions.enabled === false);
}
argCheck(options) {
if (options.draw === 'heatmap') {
if (options.strokeStyle) {
console.warn('[heatmap] options.strokeStyle is discard, pleause use options.strength [eg: options.strength = 0.1]');
}
}
}
getContext() {
return this.canvasLayer.canvas.getContext(this.context);
}
clear(context) {
context && context.clearRect && context.clearRect(0, 0, context.canvas.width, context.canvas.height);
}
}