Attaching listener to KML layer:
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 file is valid (checked by validation api), you may find it here. Each placemark in XML has id attribute like:
<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>
When clicking on a placemark in google map object returns right id, but sometimes about 50% of the time obj.featuredData.id
is null
(ZERO_RESULTS
status is in status
field). I have tried different sets of data (form 100 points to 1000), but it doesn't help. Also I've tried different precisions for lat, lng.
You've probably hit one the limits listed on that page: https://developers.google.com/kml/documentation/mapsSupport?hl=en
Maximum 1000 features per file could be the one that is bothering you.
来源:https://stackoverflow.com/questions/11775801/google-map-kml-layer-placemark-click-event-return-zero-results