Javascript Google Maps API v3事件鼠标盖和InfoBox插件
我正在使用Google Maps API的v3,并结合使用InfoBox插件(套件的一部分)来创建一些风格良好的信息窗口,以响应标记交互 在这个特定的实验中,我试图在标记器悬停在上方并弹出时弹出InfoBox窗口,但是我一直在努力解决事件系统中关于InfoBox窗口上mouseover/mouseout的问题。我可以找到DIV并使用Javascript Google Maps API v3事件鼠标盖和InfoBox插件,javascript,google-maps,Javascript,Google Maps,我正在使用Google Maps API的v3,并结合使用InfoBox插件(套件的一部分)来创建一些风格良好的信息窗口,以响应标记交互 在这个特定的实验中,我试图在标记器悬停在上方并弹出时弹出InfoBox窗口,但是我一直在努力解决事件系统中关于InfoBox窗口上mouseover/mouseout的问题。我可以找到DIV并使用google.maps.event.addDomainListener将mouseover和mouseout事件附加到信息框中,但这太麻烦了——当我将鼠标移到信息框中
google.maps.event.addDomainListener
将mouseover和mouseout事件附加到信息框中,但这太麻烦了——当我将鼠标移到信息框中的子节点上时,它算作父节点上的mouseout并触发事件
这是否与传播有关?我知道当您创建一个新的InfoBox时,InfoBox有一个enableEventPropagation
开关,但我不太清楚它将如何使用以及为什么使用
这个实验的目的是创建一个信息窗口,里面有相关的链接,显示在标记的鼠标上方。然后,您可以在信息窗口内移动鼠标并进行交互,当您将鼠标移出时,它将关闭信息窗口。我尝试了另一种方法,在该方法中,标记上的鼠标悬停触发一个外部函数,该函数创建一个外部信息窗口元素,该元素被定位并具有自己的事件处理。这很好,但地图顶部自定义信息窗口的分层意味着当您将鼠标悬停在另一个标记上时(在自定义信息窗口下),它无法为该标记注册鼠标悬停
这是我对InfoBox方法的尝试:
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
<!--
#map {
width: 800px;
height: 600px;
margin: 50px auto;
}
.map-popup {
overflow: visible;
display: block;
}
.map-popup .map-popup-window {
background: #fff;
width: 300px;
height: 140px;
position: absolute;
left: -150px;
bottom: 20px;
}
.map-popup .map-popup-content {
padding: 20px;
text-align: center;
color: #000;
font-family: 'Georgia', 'Times New Roman', serif;
}
-->
</style>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript" src="http://google-maps-utility-library-v3.googlecode.com/svn/trunk/infobox/src/infobox_packed.js"></script>
<script type="text/javascript">
var gmap, gpoints = [];
function initialize() {
gmap = new google.maps.Map(document.getElementById('map'), {
zoom: 9,
streetViewControl: false,
scaleControl: false,
center: new google.maps.LatLng(-38.3000000,144.9629796),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
for ( var i=0; i<5; i++ ) {
gpoints.push( new point(gmap) );
}
}
function popup(_point) {
_point.popup = new InfoBox({
content: _point.content,
pane: 'floatPane',
closeBoxURL: '',
alignBottom: 1
});
_point.popup.open(_point.marker.map, _point.marker);
google.maps.event.addListener(_point.popup, 'domready', function() {
// Have to put this within the domready or else it can't find the div element (it's null until the InfoBox is opened)
google.maps.event.addDomListener(_point.popup.div_, 'mouseout', function() {
_point.popup.close();
});
});
}
function point(_map) {
this.marker = new google.maps.Marker({
position: new google.maps.LatLng(-37.8131869 - (1 * Math.random()),144.9629796 + (1 * Math.random())),
map: _map
});
this.content = '<div class="map-popup"><div class="map-popup-window"><div class="map-popup-content"><a href="http://www.google.com/">Just try to click me!</a><br/>Hovering over this text will result in a <code>mouseout</code> event firing on the <code>map-popup</code> element and this will disappear.</div></div>';
// Scope
var gpoint = this;
// Events
google.maps.event.addListener(gpoint.marker, 'mouseover', function() {
popup(gpoint);
});
}
</script>
</head>
<body onload="initialize()">
<div id="map"></div>
</body>
</html>
因此,我想如果有人知道如何使这项工作,并作出适当的反应(或提供相关的提示/技巧),那么这将是伟大的
发生的情况是,我可以找到DIV并使用google.maps.event.adddomstener将mouseover和mouseout事件附加到信息框,但这太麻烦了——当我将鼠标移到信息框中的子节点上时,它将作为父节点上的mouseout并触发事件
克服这一问题的一种简单方法是遍历mouseout
处理程序中的元素树,并检查是否在文档根中找到DIV或结束。如果找到DIV,鼠标仍在弹出窗口中,您无需关闭它
jQuery通过引入第二个事件很好地解决了这个问题,该事件的行为与您预期的一样,并以类似于上面解释的方式实现。对于这种情况,有一个带水泥的特殊封口,可能值得一看。我也有同样的问题。正如您所说,问题是当移动到其中一个子元素时会触发mouseout。解决方案是使用mouseenter和mouseleave(需要jQuery),有关更多信息,请参阅本文: 在这种情况下,不可能使用google maps事件侦听器(它不支持mouseenter)。相反,您可以附加一个普通的jQuery事件,或者使用如下代码所示的hover函数:
google.maps.event.addListener(_point.popup, 'domready', function() {
//Have to put this within the domready or else it can't find the div element (it's null until the InfoBox is opened)
$(_point.popup.div_).hover(
function() {
//This is called when the mouse enters the element
},
function() {
//This is called when the mouse leaves the element
_point.popup.close();
}
);
});
你有没有找到解决这个问题的好办法?下面的jQuery示例看起来像是针对弹出窗口本身,而不是标记。我很难看到如何使用InfoBox以这种方式实现它。通过升级到InfoBox的最新开发版本并启用事件传播,我能够使弹出窗口不跳转到弹出窗口下面的任何底层标记。感谢这对我的帮助!在悬停函数中,我设置了一个变量来指示弹出窗口是否打开。然后在标记的“mouseover”回调中,我只在信息框尚未打开时显示它。最后,我添加了一个“clickclose”事件来重置指示弹出窗口关闭的变量。当Infobox覆盖一个标记时,它最终可以正常工作。最近使用jquery的方法是:
google.maps.event.addListener(_point,'domready',function(){$(_point.div)。mouseover(function(e){…