首页 > 解决方案 > 在地图 API 中将点击处理程序添加到 geojson

问题描述

有没有办法将 GeoJson 与点击事件相关联?

  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"
  }

对于普通实体,例如LatLngBounds,似乎有addListener(instance, eventName, handler)。但是,上面代码片段中的功能不存在此功能。

geojson 功能是否有等效的替代方案?

标签: google-mapsgoogle-maps-api-3

解决方案


没关系,我刚刚找到了这个帮助页面,它提供了一个关于如何做到这一点的片段:

// 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');
});

推荐阅读