点击Google地图中KML区域的活动

时间:2018-06-04 13:26:23

标签: javascript jquery google-maps events google-maps-api-3

我有一个通过Fusion Tables创建的Google地图,其中显示了大量的KML多边形。当您点击一个时,会加载通常的Google“infowindow”。但是,这需要很长时间,所以我想添加一个加载屏幕。要做到这一点,我需要使用javascript检测地图中KML区域的点击。这可能吗?

这不是我的地图,但与它非常相似:Example

这是我尝试过的:

layer = new google.maps.FusionTablesLayer({
        map: map,
        heatmap: { enabled: false },
        query: {
            // query is being placed properly here
        }
    });

    // this doesn't work below
    google.maps.event.addListener(layer, 'kmlClickEvent', function() {
        console.log('KML Clicked');
    });

1 个答案:

答案 0 :(得分:0)

事件名称不是kmlClickEvent(即返回的对象),事件为click

这对我有用:

google.maps.event.addListener(layer, 'click', function() {
    console.log('KML Clicked');
});

proof of concept fiddle

代码段

function initMap() {
  var map = new google.maps.Map(document.getElementById('map'), {
    center: {
      lat: 41.850033,
      lng: -87.6500523
    },
    zoom: 11
  });

  var layer = new google.maps.FusionTablesLayer({
    query: {
      select: '\'Geocodable address\'',
      from: '1mZ53Z70NsChnBMm-qEYmSDOvLXgrreLTkQUvvg'
    }
  });
  layer.setMap(map);
  google.maps.event.addListener(layer, 'click', function() {
    console.log('KML Clicked');
  });
}
html,
body,
#map {
  height: 100%;
  width: 100%;
  margin: 0;
  padding: 0;
}
<div id="map"></div>
<script async defer src="https://maps.googleapis.com/maps/api/js?callback=initMap">
</script>