将鼠标悬停在谷歌地图中的自定义叠加层上



是否可以侦听谷歌地图(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') });
}

我做错了什么吗?还是无法侦听自定义覆盖上的鼠标悬停?

这个答案指出地图 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') });
}

最新更新