将监听器附加到KML图层:
var layer = new google.maps.KmlLayer('http://sites.google.com/site/kmlprototypes/kmls/temp.kml?dc_=' + Math.random(),
{suppressInfoWindows:true,preserveViewport:true});
layer.setMap(map);
google.maps.event.addListener(layer, 'click', function (obj) {
alert(obj.featureData.id);
});
KML文件是有效的(通过验证API检查),您可以在这里找到它。每个XML中的placemark都有类似于id属性的标识符:
<Placemark id="46">
<Style>
<IconStyle>
<Icon>
<href>
<![CDATA[http://chart.apis.google.com/chart?chf=bg,s,EAF7FE02&chxt=y&chbh=a,4,4&chs=48x48&cht=bvg&chco=FF0000,0000FF&chds=20,9048.00,0,9048.00&chd=t:8149.00|9048.00]]>
</href>
</Icon>
</IconStyle>
</Style>
<Point>
<coordinates>30.49566650390625,50.721378326416016</coordinates>
</Point>
</Placemark>
当在谷歌地图上点击一个标记时,对象会返回正确的id,但有时约50%的时间obj.featuredData.id
为null
(status
字段中显示ZERO_RESULTS
状态)。我已经尝试了不同的数据集(从100个点到1000个点),但没有帮助。我还尝试了不同的纬度和经度精度。