简体   繁体   English

如何将标记添加到带有延迟的Google地图

[英]How do I add the markers to the google maps wth delays

I have a JS array that I receive from another function; 我有一个从另一个函数收到的JS数组; I need to loop through it and add markers with delay to the map. 我需要遍历它,并向地图添加延迟标记。 Nevertheless, it seems to be skipping to the last marker. 然而,它似乎正在跳到最后一个标记。 I tested the array size and it is good I tested array values in the loop, they are good Marker function (when called without set timeout function in the loop) works fine 我测试了数组的大小,很好,我在循环中测试了数组的值,它们是好的Marker函数(当在循环中没有设置超时功能的情况下调用)可以正常工作

<script>
var map;
var PlayDatesArray=new Array();

function playAllHistoryFunction(){   
    timeDelay1=1;       
    for(i=0; i<PlayDatesArray.length; i++) {
        pddtArray = PlayDatesArray[i].split("|"); //split String using | delimiter[date]|[lat]|[lon]   
        lt1=pddtArray[1];
        ln1=pddtArray[2];
        tstamp1= pddtArray[0];
        oldtimeDelay1=timeDelay1;
        newTimeDelay=1500;
        timeDelay1=oldtimeDelay1+newTimeDelay;        
        setTimeout(function(){            
        centerMap(lt1, ln1);
        map.setZoom(14);
        addMarker(lt1, ln1, tstamp1);                
            }, timeDelay1);        
    }
}

function centerMap(lat1, lon1){
    var latlngbounds1 = new google.maps.LatLngBounds(); 
    latlngbounds1.extend(new google.maps.LatLng(lat1, lon1)); 
    map.fitBounds(latlngbounds1);  
}

function addMarker(lat, lng, name){
    var image = new google.maps.MarkerImage('images/icon-home.gif');\
    var mn = new google.maps.Marker({
        map: map,
        icon: image,
        position: new google.maps.LatLng(lat, lng),
        title: name
    });  
    map.setCenter(new google.maps.LatLng(lat, lng));     
}

</script>

As originally posted in my comment, this is a "closure in a loop" problem. 正如最初在我的评论中所述,这是一个“循环闭合”问题。 This has been discussed here before and here is a quite decisive response. 这已经在这里讨论过了, 是一个决定性的回应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM