forked from MapGIS/WebClient-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapvLayer.js
More file actions
430 lines (378 loc) · 13.2 KB
/
MapvLayer.js
File metadata and controls
430 lines (378 loc) · 13.2 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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
import L from "leaflet";
import '../core/Base';
import {
MapvBaseLayer
} from './mapv/MapvBaseLayer';
// * @class L.zondy.MapvLayer
/**
* @origin author kyle / http://nikai.us/
* @author 基础平台/创新中心 潘卓然 ParnDeedlit
* @class module:客户端可视化.MapvLayer
* @classdesc 基于leaflet的Layer对象进行的拓展
* @param map - {Object} 传入的leaflet的地图对象
* @param dataset - {MapvDataSet} 传入的mapv的属性。 <br>
* @param mapvoption - {MapvOption} 可选参数。<br>
* @param options - {Object} L.Util.setOptions(this, options);只做额外增加的字段作用<br>
* @see https://github.com/huiyan-fe/mapv/blob/master/API.md
* @example
* var options = {
size: 13,
gradient: {
0.25: "rgb(0,0,255)",
0.55: "rgb(0,255,0)",
0.85: "yellow",
1.0: "rgb(255,0,0)"
},
max: 60,
animation: {
type: 'time',
stepsRange: {
start: 0,
end: 100
},
trails: 10,
duration: 4,
},
draw: 'heatmap'
}
var mapvLayer = new L.zondy.MapvLayer(map, dataSet, options).addTo(map);
*/
export var MapvLayer = L.Layer.extend({
initialize: function (map, dataset, mapvoption, options) {
//----------------------------------------------
//此处的三个参数仅仅是对百度的封装,只做参数传递作用
this.map = map;
this.dataset = dataset || {};
this.mapvoption = mapvoption;
//----------------------------------------------
options = options || {};
//this.project = this._project.bind(this);
this.render = this.render.bind(this);
L.Util.setOptions(this, options);
this._canvas = this._createCanvas();
L.stamp(this);
// backCanvas for zoom animation
//this._backCanvas = this._createCanvas();
//this._ctx = this._canvas.getContext('2d');
/*
this.currentAnimationFrame = -1;
this.requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame ||
window.webkitRequestAnimationFrame || window.msRequestAnimationFrame || function (callback) {
return window.setTimeout(callback, 1000 / 60);
};
this.cancelAnimationFrame = window.cancelAnimationFrame || window.mozCancelAnimationFrame ||
window.webkitCancelAnimationFrame || window.msCancelAnimationFrame || function (id) {
clearTimeout(id);
};
*/
//下面5个是leaflet专属事件,clickEvent和mousemoveEvent是mapv内部自带的方法不放出来
this.innerMoveStartEvent = this.moveStartEvent.bind(this);
this.innerMoveEndEvent = this.moveEndEvent.bind(this);
this.innnerZoomstart = this.zoomStartEvent.bind(this);
this.innerViewreset = this.viewresetEvent.bind(this);
this.innerResize = this.resizeEvent.bind(this);
},
/**
* 增加数据
* @function L.zondy.MapvLayer.prototype.addData
*
* @param data - {Array} 数据.
* @param options - {Object} 只做额外增加的字段作用
*/
addData: function (data, options) {
this.mapvBaseLayer.addData(data, options);
},
/**
* 更新数据
* @function L.zondy.MapvLayer.prototype.addData
*
* @param data - {Array} 数据.
* @param options - {Object} 只做额外增加的字段作用
*/
updateData: function (data, options) {
this.mapvBaseLayer.updateData(data, options);
},
initDevicePixelRatio: function () {
this.devicePixelRatio = window.devicePixelRatio || 1;
},
_createCanvas: function () {
var canvas = document.createElement('canvas');
canvas.style.position = 'absolute';
canvas.style.top = "0px";
canvas.style.left = "0px";
canvas.style.pointerEvents = "none";
canvas.style.zIndex = this.options.zIndex || 100;
var size = this.map.getSize();
canvas.width = size.x;
canvas.height = size.y;
canvas.style.width = size.x + 'px';
canvas.style.height = size.y + 'px';
// var className = 'leaflet-tile-container leaflet-zoom-animated';
// canvas.setAttribute('class', className);
var devicePixelRatio = this.devicePixelRatio;
canvas.getContext('2d').scale(devicePixelRatio, devicePixelRatio);
if (this.mapvoption.context == '2d') {
canvas.getContext('2d').scale(devicePixelRatio, devicePixelRatio);
}
return canvas;
},
onAdd: function (map) {
// add container with the canvas to the tile pane
// the container is moved in the oposite direction of the
// map pane to keep the canvas always in (0, 0)
var tilePane = this.getPane();
var _container = L.DomUtil.create('div', 'leaflet-layer', tilePane);
_container.appendChild(this._canvas);
//_container.appendChild(this._backCanvas);
//this._backCanvas.style.display = 'none';
//tilePane.appendChild(_container);
this._container = _container;
this.mapvBaseLayer = new MapvBaseLayer(map, this.dataset, this.mapvoption, this);
this.bindEvent();
if (this.options.tileLoader) {
this._initTileLoader();
}
this._reset();
this.fire("loaded");
},
bindEvent: function () {
var map = this.map;
map.on('movestart', this.innerMoveStartEvent);
map.on('moveend', this.innerMoveEndEvent);
map.on('zoomend', this.innerMoveEndEvent);
map.on('zoomstart', this.innnerZoomstart);
map.on('viewreset', this.innerViewreset);
map.on('resize ', this.innerResize);
this.mapvBaseLayer.bindEvent();
},
unbindEvent: function () {
var map = this.map;
map.off('movestart', this.innerMoveStartEvent);
map.off('moveend', this.innerMoveEndEvent);
map.on('zoomend', this.innerMoveEndEvent);
map.off('zoomstart', this.innnerZoomstart);
map.off('viewreset', this.innerViewreset);
map.off('resize ', this.innerResize);
this.removeAllData();
this.mapvBaseLayer.unbindEvent();
},
moveStartEvent: function () {
this.mapvBaseLayer.animatorMovestartEvent();
this._unvisiable();
},
moveEndEvent: function () {
this.mapvBaseLayer.animatorMoveendEvent();
this._reset();
this._visiable();
},
zoomStartEvent: function () {
this._unvisiable();
},
viewresetEvent: function () {
this._reset();
this._visiable();
},
resizeEvent: function () {
this._reset();
this._visiable();
},
_animateZoom: function (e) {
if (!this._animating) {
this._animating = true;
}
/* var back = this._backCanvas;
back.width = this._canvas.width;
back.height = this._canvas.height;
// paint current canvas in back canvas with trasnformation
var pos = this._canvas._leaflet_pos || { x: 0, y: 0 };
back.getContext('2d').drawImage(this._canvas, 0, 0);
// hide original
this._canvas.style.display = 'none';
back.style.display = 'block';
var map = this._map;
var scale = map.getZoomScale(e.zoom);
var newCenter = map._latLngToNewLayerPoint(map.getCenter(), e.zoom, e.center);
var oldCenter = map._latLngToNewLayerPoint(e.center, e.zoom, e.center);
var origin = {
x: newCenter.x - oldCenter.x,
y: newCenter.y - oldCenter.y
};
var bg = back;
var transform = L.DomUtil.TRANSFORM; */
//bg.style[transform] = L.DomUtil.getTranslateString(origin) + ' scale(' + e.scale + ') ';
},
_endZoomAnim: function () {
//this._animating = false;
//this._canvas.style.display = 'block';
//this._backCanvas.style.display = 'none';
},
getCanvas: function () {
return this._canvas;
},
getAttribution: function () {
return this.options.attribution;
},
draw: function () {
return this._reset();
},
onRemove: function (map) {
//this._container.parentNode.removeChild(this._container);
L.DomUtil.remove(this._container);
this.unbindEvent();
this.disposeFlag = true;
},
/**
* 设置透明度
* @function L.zondy.MapvLayer.prototype.setOpacity
* @param opacity - {Number} 1.0.
*/
setOpacity: function (opacity) {
this.options.opacity = opacity;
this._updateOpacity();
return this;
},
/**
* 设置Zindex
* @function L.zondy.MapvLayer.prototype.setZIndex
* @param zIndex - {Number} 10.
*/
setZIndex: function (zIndex) {
this._canvas.style.zIndex = zIndex;
},
bringToFront: function () {
return this;
},
bringToBack: function () {
return this;
},
_reset: function () {
this.resizeCanvas();
this.fixPosition();
this.onResize();
this.render();
},
_visiable: function () {
this.getCanvas().style.display = 'block';
},
_unvisiable: function () {
this.getCanvas().style.display = 'none';
},
_updateOpacity: function () {},
_render: function () {
this.render();
},
/**
* @description 拖动的时候和放大缩小的时候自定义图层不见得会和地图的像素坐标同步计算
* 因此再绘制自定义图层的时候需要重新计算对应的位置,这里主要是对齐左上角
* */
fixPosition: function () {
/* var domPosition = L.DomUtil.getPosition(this._map.getPanes().mapPane);
console.log(domPosition);
if (domPosition) {
L.DomUtil.setPosition(this._canvas, {
x: -domPosition.x,
y: -domPosition.y
});
} */
var topleft = this.map.getBounds().getNorthWest();
var offset = this.map.latLngToLayerPoint(topleft);
var zero = this.map.latLngToLayerPoint(L.latLng(0, 0));
//这里之所以是latLngToLayerPoint的原因是
//getPixelOrigin获取的一直是投影后地图的初始化显示的左上角相对投影源点[0,0]的偏移,这个不会随着拖动更改
//参考 https://leafletjs.com/examples/extending/pixelorigin.html 中的红色角标相对绿色角标的偏移
//为了满足拖动时候实时计算因此这里使用的是地图当前范围的左上角的经纬度,再换算成像素偏移即可
var origin = this.map.getPixelOrigin();
//console.log(topleft, offset, origin, zero);
if (topleft) {
L.DomUtil.setPosition(this._canvas, {
x: offset.x,
y: offset.y
});
}
},
resizeCanvas: function () {
var size = this.map.getSize();
var canvas = this.getCanvas();
canvas.width = size.x;
canvas.height = size.y;
canvas.style.width = size.x + 'px';
canvas.style.height = size.y + 'px';
},
// use direct: true if you are inside an animation frame call
redraw: function (direct) {
this.resizeCanvas();
this.fixPosition();
if (direct) {
this.render();
} else {
this.render();
}
},
/**
* 显示图层
* @function L.zondy.MapvLayer.prototype.show
*/
show: function () {
this._visiable();
},
/**
* 隐藏图层
* @function L.zondy.MapvLayer.prototype.hide
*/
hide: function () {
this._unvisiable();
},
/**
* 销毁图层
* @function L.zondy.MapvLayer.prototype.destroy
*/
destroy: function () {
L.DomUtil.remove(this._container);
this.unbindEvent();
this.disposeFlag = true;
},
/**
* 更新图层
* @function L.zondy.MapvLayer.prototype.update
* @param opt.data - {Array} 需要更新的数据
* @param opt.options - {Object} 需要更新的样式
*/
update: function (opt) {
if (opt == undefined) {
return;
}
this.mapvBaseLayer.updateData(opt.data, opt.options);
},
/**
* @function L.zondy.MapvLayer.prototype.removeData
* @param filter - {Function} 过滤函数,返回true的保留
* @description 移除满足过滤条件的数据
* @example
* filter: function(item){
if (item.count > 10 && item.count < 50) {
return true;
} else {
return false;
}
}
*/
removeData(filter) {
this.mapvBaseLayer && this.mapvBaseLayer.removeData(filter);
},
/**
* @function L.zondy.MapvLayer.prototype.removeAllData
* @description 移除全部数据
*/
removeAllData() {
this.mapvBaseLayer.clearData();
},
onResize: function () {},
render: function () {
this.mapvBaseLayer._canvasUpdate();
//throw new Error('render function should be implemented');
}
});
export var mapvLayer = function (dataSet, mapVOptions, options) {
return new MapvLayer(dataSet, mapVOptions, options);
};
L.zondy.MapvLayer = mapvLayer;