17

This question is asked often, but never really answered well. Let's see if we can remedy it!

Event Propagation

Google allows you to bind to events in a Google Map View via their API using event handlers.

Sometimes you may bind your event handler to an event that Google itself is already bound to. Thus, when your event fires and does whatever you told it to do you may find Google also doing its own little thing at the same time.

Hmm, can I handle the event so my code runs, but stop the event from continuing on and firing Google's event handler?

You sure can! Welcome to Event Propagation (aka Event Bubbling).

Take a look at this code

Here I bind an event handler to double clicking on the Google Map:

var aListener = google.maps.event.addListener(map, 'dblclick', 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;  
    }
});

Here map is a Google Map object to bind to.

This doesn't work. The event bubbles, and the map zooms in. I don't know why.

You ask, have you read the documentation?

Indeed. The documentation says to use event.stop();

I have looked at what others are saying. This issue is exactly my problem. It was marked as fixed, but the solution does not work.

Ideas?

Workaround

A possible workaround for the doubleclick event is to disable Google's default behavior when you need it to not fire, and then re-enable it later.

You do this with the disableDoubleClickZoom argument. See the documentation here.

Here is some code to disable:

map.set("disableDoubleClickZoom", true);

Now to re-Enable:

map.set("disableDoubleClickZoom", false);

Of course, you can set the property in the MapOptions argument for when the map object is created in the first place.

Morrowind789
  • 551
  • 3
  • 7
  • 18
  • whoa the code looks horrible. gimme a sec to fix it. – Morrowind789 Jun 28 '13 at 21:06
  • I realize this question is a year old now, but just removing the event.stop() line made this work for me. event.stop() threw a TypeError: undefined is not a function, which prevented the code following from running. Removing it, the click or doubleclick didn't propagate for me. For me, it was to prevent clicking on a marker under an overlay, but I tested it with a zoom doubleclick and it stopped that, too. – metsfan May 21 '14 at 18:15
  • It doesn't seem to work for mouseover/mouseout for me, though. The mouse icon still changes from the open hand to the finger pointing when the mouse pointer is over the marker. But the click does not go through. – metsfan May 21 '14 at 18:38
  • If you make more progress with this issue, feel free to talk about it here or somewhere else and then link to it. :) – Morrowind789 May 22 '14 at 17:27
  • No more progress. When I disabled the click propagation, I could no longer click on URLs in my overlay. I changed my markers to custom overlays which helped some (no longer any markers to click on), but I can still click on existing POIs through my overlay. It's pretty annoying. I will keep trying, and post here if I come up with a solution. – metsfan May 22 '14 at 17:31

2 Answers2

3

UPDATE: Unfortunately, I had to find out that Firefox does not make the current event accessible via window.event thus this code won't work there. I haven't found a workaround for that, yet.


It turns out the fix to your code is minimal: just remove the event parameter from your event handler function, thus accessing the global window.event object inside the handler.

The following example code worked for me in IE and Chrome, but not Firefox:

google.maps.event.addListener(map, "dblclick", function(googleMapsEvent) {
    console.debug("caught double click");
    // reference the global event object
    // ignore the googleMapsEvent passed in by Google Maps!
    event.preventDefault();
});

This answer put me on the right track!

Community
  • 1
  • 1
Oliver
  • 9,239
  • 9
  • 69
  • 100
0

Not a solution but some ideas, as requested ...

event.stopPropagation() is not a generalised panacea for issues of this type for two reasons :

  • your event handler may be responding to an already bubbled event, ie it too late to prevent event handling (zoom in this case) which was triggered earlier in the bubbling cycle.
  • it may not be a bubbling issue but an event being handled by at the same level. .addListener() implements an observer pattern, meaning that new additions are added to the element's "concrete observer" queue without removing any handlers previously added by the same mechanism.

A workaround is almost undoubtedly available for both of these situations but would probably require "inside knowledge" of Google Maps to solve.

Maybe someone else knows more than I.

Beetroot-Beetroot
  • 18,022
  • 3
  • 37
  • 44
  • 2
    Thanks for the input. Currently I am just using my workaround. Maybe one day the event.stop() will work like it says it does in the documentation. – Morrowind789 Jul 03 '13 at 23:56