How can I stop Overlay mousedown event from going to map?

After handling a mousedown event on the overlay (the ExtInfoWindow), I need to make sure the map click event handler doesn't execute, or in the map click event handler, I need to determine that the event is coming from an overlay click, and not handle it. So, some way I need to not handle it twice. What am I doing wrong below? It's for the ExtInfoWindow library, so I will not post the whole thing here. Page with map is here. Click on the info window. Search for "console.log" in extinfowindow.js to see where the problem is.

  // Initialization:

  var stealEvents = ['mousedown', 'dblclick', 'DOMMouseScroll', 'onmousewheel'];
  for( i=0; i < stealEvents.length; i++ ){
        google.maps.event.addDomListener(this.container_, stealEvents[i], this.onClick_.bind(this));
  }



    if (map.ClickListener_ == null) {
      //listen for map click, close ExtInfoWindow if open
      map.ClickListener_ = google.maps.event.addListener(map, 'click',
        function(event) {
            if( map.getExtInfoWindow() != null ){
                    // problem: This executes even if the click is on the overlay!
                map.closeExtInfoWindow();
            }
            }
      );
    }


// overlay click event handler
ExtInfoWindow.prototype.onClick_ = function(e) {
   var evt = e ? e:window.event;

   evt.cancelBubble = true;
   evt.returnValue = false;

   if (evt.stopPropagation) {
      evt.stopPropagation();
   }

   if (evt.preventDefault) {
      evt.preventDefault();
   }

   evt.stop(); // from google.maps.MouseEvent

};

Upvotes: 1

Views: 2379

Answers (2)

Marcelo
Marcelo

Reputation: 9407

Execute MouseEvent.stop(); in the first handler. IE:

google.maps.event.addListener(myOverlay, 'click', function(mouseEvent){
    mouseEvent.stop();
//Handle the click here
});

See the Google Maps documentation for MouseEvent

Upvotes: 3

Nevermind... The dom listener was listening for mousedown events not click events, therefore the click wasn't being cancelled. I should have made it listen for click events.

var stealEvents = ['click', 'dblclick', 'DOMMouseScroll', 'onmousewheel'];
for( i=0; i < stealEvents.length; i++ ){
    google.maps.event.addDomListener(this.container_, stealEvents[i], this.onClick_.bind(this));
}

Upvotes: 1

Related Questions