-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCanvasLayer.js
82 lines (72 loc) · 2.16 KB
/
CanvasLayer.js
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
/**
* 一直覆盖在当前地图视野的Canvas对象
*
* @author nikai (@胖嘟嘟的骨头, [email protected])
*
* @param
* {
* map 地图实例对象
* }
*/
function CanvasLayer(options){
this.options = options || {};
this.paneName = this.options.paneName || 'labelPane';
this.zIndex = this.options.zIndex || 0;
this._map = options.map;
this.show();
}
CanvasLayer.prototype = new BMap.Overlay();
CanvasLayer.prototype.initialize = function(map){
this._map = map;
var canvas = this.canvas = document.createElement("canvas");
canvas.style.cssText = "position:absolute;"
+ "left:0;"
+ "top:0;"
+ "z-index:" + this.zIndex + ";";
this.adjustSize();
map.getPanes()[this.paneName].appendChild(canvas);
var that = this;
map.addEventListener('resize', function () {
that.adjustSize();
that.draw();
});
return this.canvas;
}
CanvasLayer.prototype.adjustSize = function(){
var size = this._map.getSize();
var canvas = this.canvas;
canvas.width = size.width;
canvas.height = size.height;
canvas.style.width = canvas.width + "px";
canvas.style.height = canvas.height + "px";
}
CanvasLayer.prototype.draw = function(){
var map = this._map;
var bounds = map.getBounds();
var sw = bounds.getSouthWest();
var ne = bounds.getNorthEast();
var pixel = map.pointToOverlayPixel(new BMap.Point(sw.lng, ne.lat));
this.canvas.style.left = pixel.x + "px";
this.canvas.style.top = pixel.y + "px";
this.dispatchEvent('draw');
this.options.update && this.options.update.call(this);
}
CanvasLayer.prototype.getContainer = function(){
return this.canvas;
}
CanvasLayer.prototype.show = function(){
if (!this.canvas) {
this._map.addOverlay(this);
}
this.canvas.style.display = "block";
}
CanvasLayer.prototype.hide = function(){
this.canvas.style.display = "none";
//this._map.removeOverlay(this);
}
CanvasLayer.prototype.setZIndex = function(zIndex){
this.canvas.style.zIndex = zIndex;
}
CanvasLayer.prototype.getZIndex = function(){
return this.zIndex;
}