how to set marker one by one in google map api v3?

a 夏天 提交于 2019-12-13 01:24:32

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!