Search code examples
javascriptgoogle-mapsgoogle-maps-api-3geojson

Google Maps API Map Click Event doesn't work when clicked on an overlay geojson layer


I've implemented a click event on the map using

google.maps.event.addListener(map, 'click', mapclick);

and everything is working fine.

I've also added a geojson layer on map. So, the problem is when I click on the overlay geojson layer (which is a polygon layer actually), the map click event not fired. Hence, my question is what should I do so that map click event also work when I click on an overlay layer on map?


Solution

  • It's not clear what you mean by "geojson layer" , but when you mean a Data-layer there are 2 options:

    1. when you don't need the click-event to be triggered for the feature(e.g. polygon)
      set the clickable-option of the layer to false:

        map.data.setStyle({clickable:false});
      
    2. when the click-event should be triggered for both, map and feature
      trigger the event for the map programmatically:

         map.data.addListener('click',function(e){
           google.maps.event.trigger(this.getMap(),'click',e);
         });