多个标记的多个标记..为所有信息窗口获取相同的内容!!? [英] Multiple infowindows for multiple markers.. getting same content for all infowindows!!?

查看:117
本文介绍了多个标记的多个标记..为所有信息窗口获取相同的内容!!?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

for(i = 0; i< x.length; i ++)>

{

xx = x [i] .getElementsByTagName(Lat);

out1 = xx [0] .firstChild.nodeValue;

xx = x [i] .getElementsByTagName(long);

out2 = XX [0] .firstChild.nodeValue;

XX = X [i]于.getElementsByTagName( 姓名);

OUT3 = XX [0] .firstChild.nodeValue ;

XX = X [i]于.getElementsByTagName( PHONENO);

OUT4 = XX [0] .firstChild.nodeValue;

markers [i] = new google.maps.Marker({

position:{lat:parseFloat(out1),lng:parseFloat(out2)},

map:map

});

var infowindow = new google.maps.InfoWindow({

content:'

Donor Location:'+ markers [i] .getPosition()+'姓名'+''+ out3 +'Phoneno'+ out4 +''+'

for (i=0;i<x.length;i++)>
{
xx=x[i].getElementsByTagName("Lat");
out1=xx[0].firstChild.nodeValue;
xx=x[i].getElementsByTagName("long");
out2=xx[0].firstChild.nodeValue;
xx=x[i].getElementsByTagName("Name");
out3=xx[0].firstChild.nodeValue;
xx=x[i].getElementsByTagName("Phoneno");
out4=xx[0].firstChild.nodeValue;
markers[i] = new google.maps.Marker({
position: { lat:parseFloat(out1), lng: parseFloat(out2)},
map: map
});
var infowindow = new google.maps.InfoWindow({
content: '

Donor Location:' + markers[i].getPosition() +'Name'+' '+out3+'Phoneno'+out4+' '+ '

推荐答案

首先:

这不应该在for循环中:

First:
This should not be inside the for loop:
google.maps.event.addDomListener(window, 'load', initialize);



第二名:


Second:

var infowindow = new google.maps.InfoWindow({



Javascript在块中没有范围,所以此声明在一个包含函数内部。看看这个:

http:/ /www.sitepoint.com/demystifying-javascript-variable-scope-hoisting/ [ ^ ]

我建议您也了解闭包。





所以,为了这个工作你可以做类似的事情:




Javascript doesn't have scope in blocks, so this declaration is inside a containing function. Take a look at this:
http://www.sitepoint.com/demystifying-javascript-variable-scope-hoisting/[^]
and i suggest you learn about closures too.


So, for this to work you could do something like:

for (i=0;i<x.length;i++)
{
  // Here i'm declaring a function and executing it immediately,
  // creating a new scope, in which i can declare new var without affecting others.
  (function (i) {
    xx=x[i].getElementsByTagName("Lat");
    out1=xx[i].firstChild.nodeValue;
    xx=x[i].getElementsByTagName("long");
    out2=xx[i].firstChild.nodeValue;
    xx=x[i].getElementsByTagName("Name");
    out3=xx[i].firstChild.nodeValue;
    xx=x[i].getElementsByTagName("Phoneno");
    out4=xx[i].firstChild.nodeValue;

    var marker = new google.maps.Marker({
                  position: { lat:parseFloat(out1), lng: parseFloat(out2)},
                  map: map
                 }),
        infowindow = new google.maps.InfoWindow({
                          content: '
                             Donor Location:' + marker.getPosition() +'Name'+' '+out3+'Phoneno'+out4+' '+ '
  
                          '
    });
    google.maps.event.addListener(marker, 'click', function() {
      infowindow.open(map, marker);
    });
  }(i))
}





------------- - - - - - - - - -编辑 - - - - - - - - - - - - - - - - --------------

之前我没有注意到的东西,但在out1,out2,out3上设置值时你没有使用vari ,out4 vars。你总是从数组的第一个元素得到它。



------------------------------EDIT----------------------------------------------
There's something i didn't noticed before, but you are not using the var "i" when setting values on out1, out2, out3, out4 vars. You always get it from the first element on array.


这篇关于多个标记的多个标记..为所有信息窗口获取相同的内容!!?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆