How to get the event to use event.stopPropagation()?

Go To StackoverFlow.com

3

I am using the Markercluster plugin for Google Maps API V3. I want to access the click event when the user clicks on the cluster icon. The closest that I can come to is

JS Code

google.maps.event.addListener(mc, "clusterclick", function (cluster) {
    event.stopPropagation();
});

Problem: event.stopPropagation() only works this way in Chrome and not Firefox or IE. It can only work if it is passed a event object is added as a parameter to the function like so:

$("#div").click(function(event) { 
    event.stopPropagation();
}

However, I dont know the DOM element of the cluster icon created by MarkerClusterer so I cant select it!! What should I do?

2012-04-04 22:36
by Nyxynyx


3

See here: https://developers.google.com/maps/documentation/javascript/events#EventArguments

google.maps.event.addListener(map, 'click', function(event) { placeMarker(event.latLng); });

The first parameter for the event calback is the event object. In you case it would be:

google.maps.event.addListener(mc, "clusterclick", function (cluster) {     
    cluster.stopPropagation();     
});

Since this is a custom event and the programmers didn't passed the event object as a parameter, your solution would be to implement it yourself:

Lines 150 and 151 from http://code.google.com/p/google-maps-utility-library-v3/source/browse/trunk/markerclustererplus/src/markerclusterer.js?r=362:

from:

google.maps.event.trigger(mc, "click", cClusterIcon.cluster_);
google.maps.event.trigger(mc, "clusterclick", cClusterIcon.cluster_); // deprecated name 

to:

google.maps.event.trigger(mc, "click", e, cClusterIcon.cluster_);
google.maps.event.trigger(mc, "clusterclick", e, cClusterIcon.cluster_); // deprecated name 

Note the e as the 3rd parameter. This is the event object from the original event that calls this 2 lines on line 139:

google.maps.event.addDomListener(this.div_, "click", function (e) {
2012-04-04 22:55
by rcdmk
Thanks! I tried it and got the error in the console Uncaught TypeError: Object #<s> has no method 'stopPropagation'. I'm actually using methods like cluster.getCenter() which dont exist for the usual google maps event listener callback. The source code is here (http://code.google.com/p/google-maps-utility-library-v3/source/browse/trunk/markerclustererplus/src/markerclusterer.js?r=362) but I'm too new to really understand it enough. I do see clusterIcon defined in there... maybe that can help - Nyxynyx 2012-04-04 23:01
I see... This is a custom event and the event object is not passed along with the custom event trigger. If you can modify it, you can pass in the event. I will edit my answer - rcdmk 2012-04-04 23:11
Does not work for me. Event though I do eventually have access to the mouse event, the stopPropagation() and preventDefault() do not prevent the default click functionality : - mgPePe 2013-10-31 12:04
You can try to use event.returnValue = false; instead. I can't confirm this now. If you get this to work, please, post the solution here - rcdmk 2013-11-06 10:35
I've tried this solution and it works perfectly, thanks @rcdmk - 3oheme 2015-12-16 14:29


1

You can try to use return false;. Although this both stops event bubbling as well as default behaviour. So I don't know whether you can use it in your specific case.

UPDATE

clusterclick event is deprecated. You should use the click event.

Have you already tried both doing .stopPropagtion and .cancelBubble = true?

2012-04-04 22:40
by PeeHaa
I have some other code that needs to run after event bubbling is stopped. I think return false; will not be suitabl - Nyxynyx 2012-04-04 22:44
return false just exits the current function. So if you add it to the end of that event callback it may just work. If not I will delete my answer and bow my head in shame ; - PeeHaa 2012-04-04 22:45
Thanks but it didnt work.. I think after all the code in that function runs and reaches the return false; statement, the event may have bubbled to its paren - Nyxynyx 2012-04-04 22:51
That is strange. Can you setup a fiddle so I can have a look at what is going on - PeeHaa 2012-04-04 22:55
I setup a fiddle here http://jsfiddle.net/MQHyh/2/, but thats just a direct copy of part of the JS code just in case I miss out something while simplifyin - Nyxynyx 2012-04-04 23:05
@Nyxynyx updated answer - PeeHaa 2012-04-04 23:13
event.cancelBubble = true in place of event.stopPropagation without having event passed will generate a event is not defined error in FF and IE, but works in Chrome. Let me try using the MarkerclustererPlus that you linked too... seem like it has code that stops event propagation to the map!! - Nyxynyx 2012-04-04 23:35
The Plus version stops event propagation to the map perfectly - Nyxynyx 2012-04-04 23:49
Ads