【发布时间】:2012-07-31 07:49:53
【问题描述】:
将监听器附加到 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 检查),您可能会发现它here。 XML 中的每个地标都有 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(ZERO_RESULTS 状态在status 字段中)。我尝试了不同的数据集(从 100 点到 1000 点),但没有帮助。我还尝试了 lat、lng 的不同精度。
【问题讨论】:
-
只有custom markers (the "charts") 有问题吗?
-
您的 KML 似乎包含太多自定义标记。 Here 它是使用 geoxml3 渲染的(需要一段时间才能渲染)。如果隐藏这些标记(单击隐藏按钮),然后显示 KmlLayer,您会看到默认蓝色标记和自定义标记的组合。
标签: javascript google-maps-api-3 kml