可能的重复:
使用 Google Maps API v3 循环遍历标记问题 https://stackoverflow.com/questions/2670356/looping-through-markers-with-google-maps-api-v3-problem
我有一个循环,在其中从数组元素中提取一些数据,然后为每次迭代添加一个事件侦听器。但是我需要将这些值传递给每个关联的侦听器,但最终侦听器中只有最后一个数组项值。我试图理解"this" https://stackoverflow.com/questions/7809009/google-maps-event-listeners-not-working-properly-in-a-javascript-for-loop但该解决方案似乎不适合我的情况,我很困惑。这是我的代码:
for(var i=0;i<route.length;i++)
{
var input= route[i];
var sdata= input.split("@",19)
//Some code
google.maps.event.addListener(marker,'click',showiwindow);
}
function showiwindow(event)
{
var input= event.latLng;
//some code
...
document.getElementById('<%= lbl1.ClientID %>').innerHTML = sdata[1];
document.getElementById('<%= lbl2.ClientID %>').innerHTML = sdata[2];
document.getElementById('<%= lbl3.ClientID %>').innerHTML = sdata[3];
document.getElementById('<%= lbl4.ClientID %>').innerHTML = sdata[4];
......
}
当我单击标记时,我只获得最后一次迭代的值。回到该链接中的解决方案,如何传递数组以及迭代值i
。请参考我的意思的链接。
像这样的事情怎么样?
for(var i=0;i<route.length;i++)
{
var input= route[i];
var sdata= input.split("@",19)
//Some code
showiwindow(sdata);
}
function showiwindow(sdata)
{
google.maps.event.addListener(marker,'click', function(event) {
var input= event.latLng;
//some code
...
document.getElementById('<%= lbl1.ClientID %>').innerHTML = sdata[1];
document.getElementById('<%= lbl2.ClientID %>').innerHTML = sdata[2];
document.getElementById('<%= lbl3.ClientID %>').innerHTML = sdata[3];
document.getElementById('<%= lbl4.ClientID %>').innerHTML = sdata[4];
......
});
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)