是否可以在Google地图(API v3)中的自定义叠加层上收听鼠标悬停事件?一个简单的例子:
function HWPMarker(map, coords, text) { […] }
HWPMarker.prototype = new google.maps.OverlayView();
HWPMarker.prototype.draw = function() { […] }
HWPMarker.prototype.onAdd = function() {
$(this.getPanes().overlayLayer).append(this.marker); // this.marker is a div
// THIS IS WHERE I TRY TO LISTEN TO THE MOUSEOVER EVENT
google.maps.event.addListener(this, 'mouseover', function(){ alert('mouseover') });
}
我做错了吗?或者是否无法在自定义叠加层上监听鼠标悬停?
答案 0 :(得分:4)
This answer指出Maps API v3不再接受鼠标事件。因此,我们要做的是将DOM元素添加到overlayMouseTarget
并使用Google Maps DOM侦听器。这是它的工作原理:
HWPMarker.prototype.onAdd = function() {
this.getPanes().overlayMouseTarget.appendChild(this.marker); // this.marker = my dom el
google.maps.event.addDomListener(this.marker, 'mouseover', function(){ alert('mouseover') });
}