John,
I might be wrong, but I don't think it's because you used a function (or closures) that you solved the problem... but because you re-declare your array each time in the loop (inside a function in your example):
function addLineWithClosure(coordinates,i) {
var points = coordinates.split(' ',2);
var p0 = points[0].split(',',3);
var p1 = points[1].split(',',3);
var linePos = new Array();
var lineOptions = { strokeColor: "#FF0000",
strokeOpacity: 1.0,
strokeWeight: 2,
zIndex: i
};
linePos[0] = new google.maps.LatLng(Number(p0[1]),Number(p0[0]));
linePos[1] = new google.maps.LatLng(Number(p1[1]),Number(p1[0]));
var line = new google.maps.Polyline(lineOptions);
line.setPath(linePos);
line.setMap(map);
}
for (i=0; i < placemarks.length; i++) {
markName = placemarks[i].getElementsByTagName("name" )[0].childNodes[0].nodeValue;
coordinates = placemarks[i].getElementsByTagName("coordinates")[0].childNodes[0].nodeValue;
addLineWithClosure(coordinates,i);
}
You re-declare your array each time in the loop (in your function):
var linePos = new Array();
It's exactly what I'm experiencing.
If I re-declare my array before adding each polyline (in a loop or not), it works:
var points=[];
If I declare my array once, and empty it before using it again:
points.length=0;
It doesn't work the first time... but it works every time after (if just the polylines are reloaded and not the whole page).
I think we are experiencing the same issue (and I've found other people on the web with that problem as well).
Now, until I find a better solution, I re-declare my array each time in the loop (or I could use a function like you that create a new array every time) but I still don't understand why it behaves like this with polylines and not with markers...
Larry,
Mike Williams' tutorials helped me a lot when I was designing my application with v2 last year.
I hope he'll do the same with v3 :)
Thanks to both of you!