Luk*_*kas 1 jquery events google-maps-api-3 custom-overlay
是否可以在 Google Maps (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') });
}
Run Code Online (Sandbox Code Playgroud)
难道我做错了什么?或者无法在自定义叠加层上监听鼠标悬停?
这个答案指出 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') });
}
Run Code Online (Sandbox Code Playgroud)