5

I have a click event binded on an element which contains a Google Map. When the user close an InfoWindow on the map clicking on the cross, the click event is fired and propagate all over the DOM. I want to stop this event propagation, so the infoWindow closes but the click event is no raised on my container.

Google Maps API provide a closeclick event which is raised when the InfoWindow is closed, however this event can't be accessed (it is not present as a parameter):

google.maps.event.addListener(infowindow, "closeclick", function() {
    // The API function doesn't have the e parameter, I can't access closeclick
});

As my container is binded to click anyway, I'm not even sure closeclick.stopPropagation() would have worked. So I'm looking for a way to access the click event thrown by Google Map when the user clicks the cross. Of course, the html of google maps is so messed up I can't access the cross DOM element to add an eventListener to it (it's only divs everywhere, without any classes).

Any suggestions?

Edit: As a workaround, I fixed this problem by modifying my own code and testing where the event comes from when it is catched by my container:

container.addEventListener('click', function(ev) {
    if (ev.target.src == 'https://maps.gstatic.com/mapfiles/api-3/images/mapcnt6.png') {
        return false;
    }
    // the code I want to execute only when the click event doesn't come from the InfoWindow
});
Fla
  • 536
  • 6
  • 23

2 Answers2

0

You can use the following code that helped me:

var aListener = google.maps.event.addListener(map, 'click', function(event) {
    // Try to prevent event propagation to the map
    event.stop();
    event.cancelBubble = true;
    if (event.stopPropagation) {
        event.stopPropagation();
    }
    if (event.preventDefault) {
        event.preventDefault(); 
    } else {
        event.returnValue = false;  
    }
});
arush
  • 11
  • 2
-1
google.maps.event.addListener(infowindow, "closeclick", function(e) {
   e.stopPropagation();
});

Does that not work?

Paul Thomas
  • 2,756
  • 2
  • 16
  • 28
  • 1
    Nope, as said, the API function doesn't have the event as a parameter. So this will throw a `e is undefined` error. – Fla Jun 16 '17 at 08:46