Adding click handler to geojson in maps API

293 views Asked by At

Is there a way to associate a GeoJson with a click event?

  const features = map.data.addGeoJson(json);
  for (const feature of features) {
    // Add styling
    map.data.overrideStyle(
        feature,{
          fillColor: 'red',
        });
    // TODO: Add a click handler for "feature"
  }

For normal entities, such as LatLngBounds, there seems to be addListener(instance, eventName, handler). This does however not exist for the feature from the snippet above.

Is there an equivalent alternative for geojson features?

1

There are 1 answers

0
panmari On BEST ANSWER

Nevermind, I just found this help page, which gives a snippet on how to do this:

// Set mouseover event for each feature.
map.data.addListener('mouseover', function(event) {
  // "event" has the feature as a property.
  document.getElementById('info-box').textContent =
      event.feature.getProperty('letter');
});