-4

这是创建一个谷歌地图,在谷歌地图中一次传递 n 个地址,这是不可能的,我将函数集间隔(2 个部分)传递给每个迭代函数

var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var n;
var j=1;
var array_list= new Array(n);
var array_storename=new Array(n);
function InitializeMap() 
{

    directionsDisplay = new google.maps.DirectionsRenderer();
    var latlng = new google.maps.LatLng(-34.397, 150.644);
    var myOptions =
    {
        zoom: 11,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("map"), myOptions);


    directionsDisplay.setMap(map);
            directionsDisplay.setPanel(document.getElementById('directionpanel'));

    var control = document.getElementById('control');
    control.style.display = 'block';

document.getElementById('sorttable').style.display="none"

}
    function calcRoute(dist,varab) 
    {
    n=document.getElementById('Countnumbers').value;
    var start = document.getElementById('startvalue').value;
    var end = document.getElementById(dist).innerHTML;
    // end=end.replace(/[!@#$%&^*()-+=|\/:;><~]/gi," ");

    var request = {
        origin: start,
        destination: end,
        travelMode: google.maps.DirectionsTravelMode.DRIVING
    };

    directionsService.route(request, function (response, status) 
    {


        if (status == google.maps.DirectionsStatus.OK)
         {

         document.getElementById('sorttable').style.display="block";
         document.getElementById("total").style.visibility="hidden";
            directionsDisplay.setDirections(response);
            var route = response.routes[0];
             for (var i = 0; i < route.legs.length; i++)
             {

                 var a=route.legs[i].distance.text;
                 var b=route.legs[i].duration.text;
                 var bc=a+",About :"+b;
                var lblid="Labe"+varab;
                document.getElementById(lblid).innerHTML=bc;
                var store="lblstore"+varab;
                document.getElementById(store).title=end;
                 var len=10.0;
                 var dd=route.legs[i].distance.value/1000;
                 array_list[varab]=dd;
                 array_list.sort(function(a,b){return a-b});
              }

           }


       });

    }


function Button1_onclick() 
{
    for(j=1;j<=n;j++)
{
    document.getElementById('sorttable').style.display="block";
    n=document.getElementById('Countnumbers').value;
    var ss="lblstorename"+j;
    var ss1 =document.getElementById(ss).innerHTML;
//this labels are store my addresss
    calcRoute(ss,j);

 }   
}   

//我在谷歌地图中使用谷歌地图n号码地址一旦我是setTimeout(或)在for循环中设置间隔方法就不会去

4

1 回答 1

1

为了在每次迭代之间添加 2 秒延迟,您需要递归调用 urcalcRoute(..)并将您的 for 循环代码移动到calcRoute方法中。

您的代码应如下所示 -

function Button1_onclick() {      
  document.getElementById('sorttable').style.display="block";
  n=document.getElementById('Countnumbers').value;
  var ss="lblstorename"+n;//use n insteadof j
  var ss1 =document.getElementById(ss).innerHTML;            
  calcRoute(ss,j);//only your first call is inside this method. rest all will be recurrsive
}

// making your calcRoute recurrsive
function calcRoute(ss, j){
  //your code here
  if(--j>0){           
     var ss="lblstorename"+j;             
     var ss1 =document.getElementById(ss).innerHTML;//not sure why are u using ss1
     setTimeout(function(){calcRoute(ss, j)}, 2000);//here you set your time delay
  }
}
于 2012-07-03T10:31:43.187 回答