简体   繁体   English

您如何在标记中使用事件侦听器,其事件在侦听时会在两个函数之间切换?

[英]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.

相关问题 如何将两个函数添加到一个事件监听器 - how do you add two functions to one event listener 如何向多个标记添加相同的事件侦听器,然后区分 Google Maps API v3 中侦听器中的标记? - How do I add same event listener to many markers and then differentiate between the markers in the listeners in Google Maps API v3? 在 Chrome 扩展中,当 URL 的一部分是动态的时,如何为 URL 添加事件侦听器? - Within a Chrome Extension how do you add an event listener for a URL when part of the URL is dynamic? 如何添加一个事件监听器,该事件监听器在tinymce get函数被调用时触发? - How do you add an Event Listener that fires when tinymce get function gets called? 如何删除在TypeScript中使用“this”的事件侦听器? - How do you remove an event listener that uses “this” in TypeScript? 如何在具有事件侦听器的 div 的两个背景图像之间切换 - How to toggle between two background images for a div having an event listener 刺激控制器:事件监听多次; 如何删除事件侦听器并保留上下文? - Stimulus controller: event listened multiple times; how do I remove event listeners and retain the Context? 通过侦听器事件放置时,Google地图标记未显示 - Google map markers not showing up when placed through listener event 您可以从事件侦听器一次触发两个或多个函数吗? - Can you fire off two or more functions at once from an event listener? 如何在JS中使用事件监听器 - How to use event listener in JS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM