Adding click event to KMLLayer Placemarks and Markers

跟風遠走 提交于 2019-12-18 12:29:22

问题


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?


回答1:


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);
});



回答2:


Seemingly the onlick event is wrapped up when the kml loads (GMaps v3, kml with Placemarks) Any Placemark references to "BallonStyle" bundled in the same kml file causes these to replace the default popup - and you can achieve a lot with them.

These are the kml elements supported by Gmaps v3 http://code.google.com/apis/kml/documentation/kmlelementsinmaps.html

If your question is how to intercept that onlick event, then I am sorry I do not know how you can achieve that.



来源:https://stackoverflow.com/questions/6070837/adding-click-event-to-kmllayer-placemarks-and-markers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!