提交 | 用户 | 时间
|
58d006
|
1 |
GMaps.prototype.drawOverlay = function(options) { |
A |
2 |
var overlay = new google.maps.OverlayView(), |
|
3 |
auto_show = true; |
|
4 |
|
|
5 |
overlay.setMap(this.map); |
|
6 |
|
|
7 |
if (options.auto_show != null) { |
|
8 |
auto_show = options.auto_show; |
|
9 |
} |
|
10 |
|
|
11 |
overlay.onAdd = function() { |
|
12 |
var el = document.createElement('div'); |
|
13 |
|
|
14 |
el.style.borderStyle = "none"; |
|
15 |
el.style.borderWidth = "0px"; |
|
16 |
el.style.position = "absolute"; |
|
17 |
el.style.zIndex = 100; |
|
18 |
el.innerHTML = options.content; |
|
19 |
|
|
20 |
overlay.el = el; |
|
21 |
|
|
22 |
if (!options.layer) { |
|
23 |
options.layer = 'overlayLayer'; |
|
24 |
} |
|
25 |
|
|
26 |
var panes = this.getPanes(), |
|
27 |
overlayLayer = panes[options.layer], |
|
28 |
stop_overlay_events = ['contextmenu', 'DOMMouseScroll', 'dblclick', 'mousedown']; |
|
29 |
|
|
30 |
overlayLayer.appendChild(el); |
|
31 |
|
|
32 |
for (var ev = 0; ev < stop_overlay_events.length; ev++) { |
|
33 |
(function(object, name) { |
|
34 |
google.maps.event.addDomListener(object, name, function(e){ |
|
35 |
if (navigator.userAgent.toLowerCase().indexOf('msie') != -1 && document.all) { |
|
36 |
e.cancelBubble = true; |
|
37 |
e.returnValue = false; |
|
38 |
} |
|
39 |
else { |
|
40 |
e.stopPropagation(); |
|
41 |
} |
|
42 |
}); |
|
43 |
})(el, stop_overlay_events[ev]); |
|
44 |
} |
|
45 |
|
|
46 |
google.maps.event.trigger(this, 'ready'); |
|
47 |
}; |
|
48 |
|
|
49 |
overlay.draw = function() { |
|
50 |
var projection = this.getProjection(), |
|
51 |
pixel = projection.fromLatLngToDivPixel(new google.maps.LatLng(options.lat, options.lng)); |
|
52 |
|
|
53 |
options.horizontalOffset = options.horizontalOffset || 0; |
|
54 |
options.verticalOffset = options.verticalOffset || 0; |
|
55 |
|
|
56 |
var el = overlay.el, |
|
57 |
content = el.children[0], |
|
58 |
content_height = content.clientHeight, |
|
59 |
content_width = content.clientWidth; |
|
60 |
|
|
61 |
switch (options.verticalAlign) { |
|
62 |
case 'top': |
|
63 |
el.style.top = (pixel.y - content_height + options.verticalOffset) + 'px'; |
|
64 |
break; |
|
65 |
default: |
|
66 |
case 'middle': |
|
67 |
el.style.top = (pixel.y - (content_height / 2) + options.verticalOffset) + 'px'; |
|
68 |
break; |
|
69 |
case 'bottom': |
|
70 |
el.style.top = (pixel.y + options.verticalOffset) + 'px'; |
|
71 |
break; |
|
72 |
} |
|
73 |
|
|
74 |
switch (options.horizontalAlign) { |
|
75 |
case 'left': |
|
76 |
el.style.left = (pixel.x - content_width + options.horizontalOffset) + 'px'; |
|
77 |
break; |
|
78 |
default: |
|
79 |
case 'center': |
|
80 |
el.style.left = (pixel.x - (content_width / 2) + options.horizontalOffset) + 'px'; |
|
81 |
break; |
|
82 |
case 'right': |
|
83 |
el.style.left = (pixel.x + options.horizontalOffset) + 'px'; |
|
84 |
break; |
|
85 |
} |
|
86 |
|
|
87 |
el.style.display = auto_show ? 'block' : 'none'; |
|
88 |
|
|
89 |
if (!auto_show) { |
|
90 |
options.show.apply(this, [el]); |
|
91 |
} |
|
92 |
}; |
|
93 |
|
|
94 |
overlay.onRemove = function() { |
|
95 |
var el = overlay.el; |
|
96 |
|
|
97 |
if (options.remove) { |
|
98 |
options.remove.apply(this, [el]); |
|
99 |
} |
|
100 |
else { |
|
101 |
overlay.el.parentNode.removeChild(overlay.el); |
|
102 |
overlay.el = null; |
|
103 |
} |
|
104 |
}; |
|
105 |
|
|
106 |
this.overlays.push(overlay); |
|
107 |
return overlay; |
|
108 |
}; |
|
109 |
|
|
110 |
GMaps.prototype.removeOverlay = function(overlay) { |
|
111 |
for (var i = 0; i < this.overlays.length; i++) { |
|
112 |
if (this.overlays[i] === overlay) { |
|
113 |
this.overlays[i].setMap(null); |
|
114 |
this.overlays.splice(i, 1); |
|
115 |
|
|
116 |
break; |
|
117 |
} |
|
118 |
} |
|
119 |
}; |
|
120 |
|
|
121 |
GMaps.prototype.removeOverlays = function() { |
|
122 |
for (var i = 0, item; item = this.overlays[i]; i++) { |
|
123 |
item.setMap(null); |
|
124 |
} |
|
125 |
|
|
126 |
this.overlays = []; |
|
127 |
}; |