Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if click is inside OverlayView

I want to display a certain Infobox when the user clicks anywhere on the map and a different Infobox when the user clicks inside an OverlayView.

I'm adding a listener to the click event for the map object, but this event only provides a latLong param which seems insufficient to tell if an OverlayView was hitted.

    google.maps.event.addDomListener(map, 'click', function(param) {
        // if( an OverlayView was clicked)
        // showInfoboxForOverlayView();
        // else
        // showStandarInfobox();
    });

I know I can add a separate listener for the OverlayView object, but when I do this, both events are fired (the one coming from the OverlayView object and the one from the map object).

This is how I construct my OverlayView object,

    var overlay = new google.maps.OverlayView();
    overlay.onAdd = function () {

        var layer = d3.select(this.getPanes().overlayMouseTarget).append("div").attr("class", "SvgOverlay");
        var svg = layer.append("svg");
        var adminDivisions = svg.append("g").attr("class", "AdminDivisions");

        overlay.draw = function () {
            var markerOverlay = this;
            var overlayProjection = markerOverlay.getProjection();

            // Turn the overlay projection into a d3 projection
            var googleMapProjection = function (coordinates) {
                var googleCoordinates = new google.maps.LatLng(coordinates[1] + 0.0005, coordinates[0] - 0.0006);
                var pixelCoordinates = overlayProjection.fromLatLngToDivPixel(googleCoordinates);
                return [pixelCoordinates.x + 4000, pixelCoordinates.y + 4000];
            }

            path = d3.geo.path().projection(googleMapProjection);
            adminDivisions.selectAll("path")
                    .data(geoJson.features)
                    .attr("d", path) // update existing paths
                    .enter().append("svg:path")
                    .attr("d", path);;
        };
    };

Any ideas?

like image 643
mcabral Avatar asked Nov 12 '22 19:11

mcabral


1 Answers

Try using the cancelBubble flag from the global event object within the handler of the overlay element:

event.cancelBubble = true;

That should prevent the second click handler from triggering.

like image 165
Germán Enríquez Avatar answered Nov 15 '22 09:11

Germán Enríquez