GalloPinto
GalloPinto

Reputation: 677

Add click event to markers from a loaded KML

I'm loading a KML to my google maps. With this functions the maps loads correctly with the markers on the kml

function initialize() {
    var mapOptions = {
        center: new google.maps.LatLng(12.136389, -86.251389),
        zoom: 11
    };
    var map = new google.maps.Map(document.getElementById("map_canvas"),
        mapOptions);

    var ctaLayer = new google.maps.KmlLayer({
        url: 'http://mobilenicode-001-site1.smarterasp.net/Content/Prueba.css'
    });
    ctaLayer.setMap(map);
}

The problem is that i'm trying to add a "addListener" events to the markers that comes from the KML. How can I achieve that?

Upvotes: 1

Views: 823

Answers (2)

geocodezip
geocodezip

Reputation: 161404

Add a click listener to the KmlLayer (as described in the documentation):

var ctaLayer = new google.maps.KmlLayer({
    url: 'http://mobilenicode-001-site1.smarterasp.net/Content/Prueba.css'
});
ctaLayer.setMap(map);
google.maps.event.addListener(ctaLayer,'click',function(evt) {
    alert("marker clicked");
});

working fiddle

Note that the only mouse event that KmlLayers support is the "click" event.

Upvotes: 1

ffflabs
ffflabs

Reputation: 17511

You can't. Google renders the KML server side and overlays it in your map with their given behavior and elements as a whole. While it's true that you can set a few options for the KML layer, you can't address particular elements inside of it.

Depending on your backend, if you have one, you could parse a KML file. (Markers should appear inside a <PlaceMark> tag, but your mileage may vary) and draw the markers yourself.

Upvotes: 1

Related Questions