Adding click event to KMLLayer Placemarks and Markers

Anirudh Ramanathan picture Anirudh Ramanathan · May 20, 2011 · Viewed 14.8k times · Source

How do i attach an onclick event to Placemarks specified in the KML file. Can event listeners be added to both google maps and google earth plugin? How would I go about this?

Answer

Anirudh Ramanathan picture Anirudh Ramanathan · May 20, 2011

In the Google Earth plugin...

google.earth.fetchKml(ge, href, function(kmlObject) {});
google.earth.addEventListener(kmlObject, 'click', function(event) {
    event.preventDefault();
    var kmlPlacemark = event.getTarget();
    alert(kmlPlacemark.getName());
});

In Google Maps API

var ctaLayer = new google.maps.KmlLayer('http://www.****.com/index.kml');
ctaLayer.setMap(map);
google.maps.event.addListener(ctaLayer, 'click', function(kmlEvent) {
    var text = kmlEvent.featureData.description;
    alert(text);
});