我如何让信息窗口停留在鼠标上的标记在谷歌地图
How do i let the infowindow stay on mouseout of the marker in google maps?
我需要信息窗口出现在鼠标上的标记。我想关闭信息窗口时,鼠标是任何地方除了信息窗口本身。我需要这个,因为,我有一个超链接在信息窗口和用户应该能够点击超链接。现在我有下面的代码:
marker.addListener('mouseover', function() {
infowindow.open(map, marker);
}, false);
marker.addListener('mouseout', function() {
infowindow.close(map, marker);
}, false);
这不起作用,因为当我从标记上移开焦点时,信息窗口就会关闭。
请告诉我。由于
你的问题有三个部分:
-
设置标记的
mouseover
事件处理程序的超时时间,以便InfoWindow不会消失,直到您将鼠标移动到它上面。 -
当鼠标出现在InfoWindow上时清除超时:
var closeInfoWindowWithTimeout; marker.addListener('mouseout', function() { closeInfoWindowWithTimeout = setTimeout(() => infowindow.close(map, marker), 1000); }, false); infoWindowElement.mouseover(() => clearTimeout(closeMarkerInfoWithTimeout));
-
在
InfoWindow
内容的父的父上设置mouseleave
事件。我知道它很脏,而且依赖于谷歌地图实现InfoWindow的方式,但这是你现在所能做的。请注意,您应该在InfoWindow附加到dom之后执行此操作!您可以通过听InfoWindow的
domready
事件来等待:infowindow.addListener('domready', () => { infowindowelement.parent().parent().parent().mouseleave(() => infowindow.close(map, marker)); });
注意你应该使用元素来初始化你的InfoWindow的内容(这里我使用变量
infoWindowElement
)
这当然不是完美的,但是看看这个:
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>
<script>
var markers = []; // store the marker objects here
var infoWindow;
var locations = [
[50, 4],
[50.5, 4.5],
[51, 5],
[51.5, 5.5]
]
var contentStrings = [
'Hello',
'World',
'Foo',
'Bar'
];
var mouseIn = false;
function initialize() {
var mapObject = {
mapTypeId: google.maps.MapTypeId.ROADMAP,
zoom: 8,
center: new google.maps.LatLng(locations[1][0], locations[1][1])
};
map = new google.maps.Map(document.getElementById("googleMap"), mapObject);
// make 1 infowindow. We will use it again and again
infoWindow = new google.maps.InfoWindow({
content: ''
});
loadMarkers();
}
google.maps.event.addDomListener(window, 'load', initialize);
function loadMarkers() {
for (var i=0; i<locations.length; i++) {
var marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][0], locations[i][1]),
map: map // replaces marker.setMap(map);
});
markers.push(marker); // store the marker in the array
google.maps.event.addListener(marker, 'mouseover', function () {
// first we want to know which marker the client clicked on.
var i=markers.indexOf(this);
// we set the content of infoWindow, then we open it.
infoWindow.setContent('<div onmouseout="mouseOutsideInfowindow()" onmouseover="mouseinsideInfowindow()">' + contentStrings[i] + '</div>')
infoWindow.open(map, markers[i]);
mouseIn = false;
});
}
}
function mouseinsideInfowindow() {
mouseIn = true;
}
function mouseOutsideInfowindow() {
if(mouseIn) {
infoWindow.close();
mouseIn = false;
}
}
</script>
<style>
#googleMap {
height: 400px;
}
</style>
<div id="googleMap"></div>
相关文章:
- 如何停止地图移动时,鼠标悬停在标记在谷歌地图API 3
- 谷歌地图 api v3 搜索多边形没有鼠标事件
- 如何在谷歌地图上用鼠标移动标记
- 在谷歌地图中用鼠标移动标记
- 谷歌地图jQuery-通过鼠标点击获取LatLng
- 重叠圆圈阻止鼠标悬停事件-谷歌地图v3
- 谷歌地图v3获取鼠标滚轮事件
- 检测谷歌地图 v3 多边形上的鼠标事件的 shiftKey
- 使用OpenLayers和谷歌地图,鼠标光标和地图上的点不是内联的
- 由谷歌地图绘制的矩形 绘图管理器不会触发鼠标移动事件
- 谷歌地图API:显示代码中的鼠标悬停窗口
- 每次点击鼠标时都会刷新谷歌地图
- 鼠标悬停,鼠标退出,然后单击谷歌地图上的同一标记
- 如何区分谷歌地图API v3中鼠标左键和右键点击鼠标向下事件
- 谷歌地图API:删除和重置鼠标悬停上的标记
- 在谷歌地图v3中显示鼠标悬停事件的工具提示
- 在有角度的谷歌地图中使用鼠标移动标记
- 在鼠标悬停上动态创建的谷歌地图只有第一次才能正确显示
- 谷歌地图-标记在鼠标悬停前闪烁
- 谷歌地图鼠标悬停与标记和数据层的行为