geoxml3在地标上添加事件

时间:2014-03-04 15:37:56

标签: geoxml3

我正在尝试在地标上添加事件但收到错误'docs is undefined'。当我尝试提醒地标的名称时,它们工作正常,为什么不发生事件?

var map = new google.maps.Map(document.getElementById("map"), mapOptions);
var geoXml = new geoXML3.parser({
        map: this.map,
        singleInfoWindow:true,
                    afterParse: this.useTheData

    });  

            geoXml.parseKmlString(<my kml string>);
    google.maps.event.addListener(this.map, "bounds_changed", RefreshMap);
    google.maps.event.addListener(this.map, "center_changed", RefreshMap);
    google.maps.event.addListener(this.map, "zoom_changed", RefreshMap);

},
 useTheData: function(doc) {
  for (var i = 0; i < doc[0].placemarks.length; i++) {
  docs[0].placemarks[i].events.add("click", function () {alert("event!!");});
  //alert(docs[0].placemarks[i].name);
}
}

1 个答案:

答案 0 :(得分:1)

“docs”未定义。本地版本是“doc”。

<。> .placemark是地标的JSON表示,而不是处理点击事件的Google地图对象。

它包含对谷歌地图对象的引用(标记的.placemark.marker),您可以添加点击事件监听器。

var map = new google.maps.Map(document.getElementById("map"), mapOptions);
var geoXml = new geoXML3.parser({
        map: this.map,
        singleInfoWindow:true,
                    afterParse: this.useTheData

    });  

    geoXml.parseKmlString(<my kml string>);
    google.maps.event.addListener(this.map, "bounds_changed", RefreshMap);
    google.maps.event.addListener(this.map, "center_changed", RefreshMap);
    google.maps.event.addListener(this.map, "zoom_changed", RefreshMap);

},
  useTheData: function(doc) {
   for (var i = 0; i < doc[0].placemarks.length; i++) {
     google.maps.event.addListener(
       doc[0].placemarks[i].marker,
       "click", 
       function () {alert("event!!");
     });
   //alert(docs[0].placemarks[i].name);
   }
}