简体   繁体   中英

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'. 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'?

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM