2

I am using the MarkerClusterer library for Google Maps API V3 to group nearby markers into a single marker. When the user clicks on the cluster marker, a div appears beside that cluster marker. Now I want to make it such that after clicking on the cluster marker, to close the div that popped up, he can click on the map.

Problem: After adding a google.maps.event.addListener(map, 'click', function(){}) on the map, when the user clicks on the cluster marker to open the popup div, nothing happens!! I'm guessing the click event on the cluster marker caused the div to popup, but the click event also propagated to the map, triggering the map's click listener to close the popup.

I wish i can use jQuery's event.stopPropagation() but where can I get the event object?

How can I solve this problem? How can I prevent the click from propagating through the cluster marker to the map? Hopefully the solution is cross-browser compatible.

JS Code

clusterclickListener = google.maps.event.addListener(mc, "clusterclick", function (cluster) {
    $('#content_container').show();
});

google.maps.event.addListener(map, 'click', function() {
    $('#content_container').hide(); 
});
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Nyxynyx
  • 61,411
  • 155
  • 482
  • 830

1 Answers1

4

Hmm. I just tested this and everything works for me. My only advice is to check if your div has correct html/css/positioning as it may simply be covered up by a map.

Event object is accessible by default in the handler function. If you want to access the event object you can simply access it using "event" so event.stopPropagation() should work. Keep in mind that if you pass "event" as an argument to the handler function you will get a custom Google maps click event which does not have stopPropagation method.

Michal
  • 13,439
  • 3
  • 35
  • 33
  • Thanks @Michal for your answer! It helped me so much that I've written a blog post on the topic: [Stop Propagation of Google Maps Marker Click Event – a Solution!](http://shades-of-orange.com/post/2015/09/18/Stop-Propagation-of-Google-Maps-Marker-Click-Event-a-Solution!) – Oliver Sep 18 '15 at 19:19