I know there’s a question about this Google Maps API v3 drop markers from XML with delay? and I followed it, but I had to made a mistake in my code, here it is:
function dropAnim(lat,lng,name) {
console.log("---- " + lat + " - " + lng);
var marker = new google.maps.Marker({
position: new google.maps.LatLng(lat,lng),
map: map,
title: name,
animation: google.maps.Animation.DROP,
icon: imageCar,
});
}
var counter = 0;
$.get('http://server.com/methods.php?m=loadPins', function(data) {
dataName = data.name.split(";");
dataLat = data.lat.split(";");
dataLng = data.lng.split(";");
dataLength = dataName.length-1;
for(var i=0;i<dataLength;i++) {
var lat = dataLat[i];
var lng = dataLng[i];
console.log(lat + " - " + lng);
setTimeout(function() {
dropAnim(lat,lng,dataName[i]);
}, counter * 400);
counter++;
}
},"json");
The problem is that it loads 3 pins in the exact same place. dataLat and dataLng load 3x times different and I can see that in console, but when I see what comes out in console from insde the function, there are 3 same lat and lng (the last one).
Try like this:
Your problem was, when your timeout’s callback is called,
ivariable already hasdataLengthvalue, as your loop is already finished by that time.