晚上好,
我使用Google Maps API动态地(for循环)将5个标记和信息框添加到地图中。现在我想动态设置监听器,我得到了以下代码:
for(var i in partnerschools){
infoContent = 'BLABLA';
info = new google.maps.InfoWindow({
content: infoContent,
maxWidth: 230
});
infos.push(info);
var marker = new google.maps.Marker({
position: partnerschools[i].coordinates,
map: map,
animation: google.maps.Animation.DROP,
title: partnerschools[i].title,
});
markers.push(marker);
markers[i].addListener('click', function(){
infos[i].open(map, markers[i]);
});
}
问题是它总是在点击其中一个标记后打开的同一个信息框。在我尝试将听众添加到每个标记之前,一切都运行良好,我做错了什么?
答案 0 :(得分:0)
解决
var addListener = function (i) {
google.maps.event.addListener(markers[i], 'click', function(){
infos[i].open(map, markers[i]);
});
}
addListener(i);