我想将谷歌地图多边形(google.maps.Polygon)和地图(google.maps.Map)封装到一个javascript对象中,并处理多边形和地图的一些事件。下面是一些代码
function Landmark(map, polygon) {
this.map = map;
this.polygon = polygon;
// Add a listener for the click event
google.maps.event.addListener(this.map, 'click', this.addPoint);
google.maps.event.addListener(this.polygon, 'click', this.addPoint);
addPoint = function (event) {
alert("xxx");
}
}我使用以下命令调用该函数:
var myLatLng = new google.maps.LatLng(24.886436490787712, -70.2685546875);
var myOptions = {
zoom: 5,
center: myLatLng,
mapTypeId: google.maps.MapTypeId.TERRAIN
};
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
polygonInTest = new google.maps.Polygon({
strokeColor: "#FF0000",
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: "#FF0000",
fillOpacity: 0.35
});
polygonInTest.setMap(map);
var landmark = new Landmark(map, polygonInTest);但是当我通过单击地图来触发事件时,我从firebug中得到了这个错误:
f.e is undefined
[Break On This Error] function de(a,b){var c,d=a.__e3_||{};i...n"+b]=c,c=new ce(a,b,c,3));return c}; 谁能指出我哪里做错了,并给出一些建议?任何意见或帮助,我们将不胜感激。
提前谢谢。
发布于 2011-05-18 02:26:56
我最好的猜测是,当您创建事件侦听器时
google.maps.event.addListener(this.map, 'click', this.addPoint);您的处理程序尚未定义(即稍后定义addPoint ),因此在执行addListener()时,调用this.addPoint为undefined。到了调用处理程序的时候,GMaps就搞砸了。
至少,通过查看您的代码,我意识到我在这一点上遇到了问题:)
https://stackoverflow.com/questions/5485543
复制相似问题