0

我正在尝试在地标上添加事件,但收到错误“文档未定义”。当我尝试提醒地标名称时,它们可以正常工作,那么为什么不显示事件呢?

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

1 回答 1

1

“文档”未定义。本地版本是“doc”。

.placemark 是地标的 JSON 表示形式,而不是处理点击事件的谷歌地图对象。

它包含对 google maps 对象(标记为 .placemark.marker)的引用,您可以向该对象添加 click 事件侦听器。

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);
   }
}
于 2014-03-04T19:24:21.940 回答