[英]How do you use an event listener in markers whose event toggles between two functions when listened to?
I have this section of code which works well:我有这部分代码运行良好:
marker.addListener('click', function(){
showInfo(marker, content);
});
marker.addListener('dblclick', function(){
hideInfo();
});
I'm using two event listeners, one listens to 'click' and the other listens to 'dblclick'.我正在使用两个事件侦听器,一个侦听“click”,另一个侦听“dblclick”。 However, I only want to use just one event listener that listens to 'click' and achieve the same results.
但是,我只想使用一个事件侦听器来侦听“单击”并获得相同的结果。 How can I toggle between
showInfo()
and hideInfo()
functions by using just one event 'click'?如何仅使用一个事件“单击”在
showInfo()
和hideInfo()
函数之间切换?
You may use a global variable to determine whether the info is visible or hidden.您可以使用全局变量来确定信息是可见的还是隐藏的。
...
var isInfoVisible;
...
then那么
marker.addListener('click', function(){
if (isInfoVisible){
isInfoVisible = false;
hideInfo();
} else {
isInfoVisible = true;
showInfo(marker, content);
}
});
OR:或:
if you use infowindow you can check if it is attached to the map.如果您使用信息窗口,您可以检查它是否附加到地图上。
marker.addListener('click', function(){
if (infowindow.map != null){
infowindow.close();
} else {
infowindow.setContent(content);
infowindow.open(map, marker);
}
});
https://jsfiddle.net/oxh0gq5w/ https://jsfiddle.net/oxh0gq5w/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.