问题
I am doing a google map which will read a set of coordinates and put the marker on the map one by one.
Below is my idea:
function A{
for loop(
set marker
call setTimeout('A',2seconds)
)
}
my idea is to set a marker and use setTimeout to wait 2 seconds and then set the next marker.
However, it doesn't work. it show all the markers at the same time and repeat to renew all markers.
How can i achieve my goal? Thanks for your help!!!!!!!!!!!!!!!
Here is my code:
function marker(){
var marker;
var i=0;
while(i<locations.length){
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
animation: google.maps.Animation.DROP,
map: map
});
i=i+1;
var t=setTimeout("marker()",2000);
}
}
marker();
回答1:
You need to return after setting the first marker. You also need a parameter to A which specifies which marker to show.
function marker(i){
if (i > locations.length) return;
var marker;
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
animation: google.maps.Animation.DROP,
map: map
});
var t=setTimeout("marker("+(i+1)+")",2000);
}
marker(0);
来源:https://stackoverflow.com/questions/6605440/how-to-set-marker-one-by-one-in-google-map-api-v3