好吧,这已经困扰我三天了,javascript 闭包不是我的强项。
我有一个谷歌地图,我在其中应用了一系列可点击多边形的叠加层。我想要一个特定的函数根据我正在工作的单击的多边形来运行。问题是我不知道如何使用监听器,以便显示一个包含该多边形邮政编码的信息窗口的函数。代码如下:
for (x = 0; x < 50 && coordsObject.length > x; x++) { //Only draw 50 polygons at a time
...
//create zipcoords array
clickablePolygons.push(new google.maps.Polygon({
paths: zipCoords
, strokeColor: "#000"
, strokeOpacity: 1
, strokeWeight: 1
, fillColor: convertRatingToHex(rating)
, fillOpacity: 0.45
}));
infoWindow.push(
new google.maps.InfoWindow({
content: '<div id="gcontent">' + zip.toString() + '</div>'
})
);
//problem child
var theFunction = function(arguments, infowindow, map) {
var marker = new google.maps.Marker({
position: arguments[0].latLng
});
infowindow.open(map, marker);
};
google.maps.event.addListener(clickablePolygons[clickablePolygons.length - 1], 'click', function() {
theFunction(arguments, infoWindow[x], map); //:(
});
clickablePolygons[clickablePolygons.length - 1].setMap(map);
}
我在关闭时做错了什么?
最佳答案
在你的 addListener 调用中,你有 function() 而不是 function(arguments)。我还会在调用 addlistener 之外创建一个指向 infoWindow 的变量。假设单击事件将传递您期望的参数。它可能需要是函数(e,参数)。
var win = infoWindow[x];
google.maps.event.addListener(clickablePolygons[clickablePolygons.length - 1], 'click', function(arguments) {
theFunction(arguments, win, map);
});
关于javascript - GoogleMaps addlistener zip 弹出窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16148296/