我正在添加像这样的事件监听器 -
var infowindow = new google.maps.InfoWindow({
content: 'Hi there from Alabama!'
});
google.maps.event.addListener(marker, 'mouseover', function () {
infowindow.open(map, marker); // this displays the infowindow on mouseover
});
google.maps.event.addListener(marker, 'mouseout', function () {
infowindow.close(); // this close the infowindow on mouseout
});
marker.addListener('click', function () {
google.maps.event.clearListeners(marker, 'mouseout');
// this removes the mouseout listener when the marker is clicked so that it stays
// present after mouseout.
});
前面提到的是一种享受,但我也希望能够在信息窗口关闭后重新添加mouseout
事件。
我尝试将其添加到我的功能中 -
google.maps.event.addListener(infowindow, 'closeclick', function () {
google.maps.event.addListener(marker, 'mouseout');
// when the info window is closed the mouseout event is reinstated
// I also previously added an alert here to make sure it was hit!
});
这会创建标记ok,但是在行动中,我可以点击标记> mouseout和infowindow保持存在>关闭infowindow。所有期望的行为。
然而,当再次悬停在标记上时,infowindow会显示(正如预期的那样)但是在点击mouseout
事件时,我得到了 -
JavaScript runtime error: Unable to get property 'apply' of undefined or null reference
错误存在于Google api JS文件中,因此很难确定问题的根源是什么。如何在mouseout
关闭时正确恢复infowindow
事件?
答案 0 :(得分:1)
目前,您在事件分配中有一个匿名函数:
google.maps.event.addListener(marker, 'mouseout', function () {
infowindow.close(); // this close the infowindow on mouseout
});
如果您创建了一个命名函数,可以这样添加:
function mouseoutHandler() {
infowindow.close(); // this close the infowindow on mouseout
}
google.maps.event.addListener(marker, 'mouseout', mouseoutHandler);
然后,您可以删除它......
marker.addListener('click', function () {
google.maps.event.clearListeners(marker, 'mouseout');
// this removes the mouseout listener when the marker is clicked so that it stays
// present after mouseout.
});
稍后,您只需像第一次那样重新添加事件 - 因为您有一个命名函数,您不会丢失声明!
如果您在删除原始活动时仍然遇到错误,请might try being more specific:
var listenerHandle = google.maps.event.addListener(marker, 'mouseout', mouseoutHandler);
// later...
google.maps.event.removeListener(listenerHandle);