Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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+' '+ '

'
});
google.maps.event.addListener(markers[i], 'click', function() {
infowindow.open(map, markers[i]);
});

google.maps.event.addDomListener(window, 'load', initialize);
}


Here i retreived latitude and longitude values from xml and using markers i plotted all points. but when i click on each marker the content of infowindow remains same. the content am getting is the last parsed xml lat long name phone but what i need is for each loc corresponding name phoneno..
what should i do ??
Posted

1 solution

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

Second:
JavaScript
var infowindow = new google.maps.InfoWindow({

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:

JavaScript
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))
}


------------------------------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.
 
Share this answer
 
v3
Comments
moyna coder 8-Sep-15 10:41am    
still i am getting the same problem!! :(
Moykn 8-Sep-15 10:43am    
Can you show me how is your code now?
Moykn 8-Sep-15 10:54am    
Please, check my updated Solution.
moyna coder 8-Sep-15 10:55am    
yeah i understood my mistake..thank you :)
i gave it as a seperate function.. it works..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900