谷歌地图上的多点之间的方向

时间:2018-08-07 10:50:46

标签: javascript maps

大家好,我想用google map将它们之间的多个点链接起来,但是问题是2点而不是3点。点的坐标位于数据库中,我使用JSON将其与另一个文件一起导入,例如,我有3个点:  点(A)=巴黎..点(B)=柏林..点(C)=布拉格 我想要:A ----> B ----> C,但是代码显示B ---> C

var xmlhttp = new XMLHttpRequest();
     xmlhttp.onreadystatechange = function() {
     if (this.readyState == 4 && this.status == 200) {
     var myObj2= JSON.parse(this.responseText);
     var mapOptions = {
     center: new google.maps.LatLng(42.5584308, -70.8597732),zoom: 3,
      };
     var map = new google.maps.Map(document.getElementById("map"), mapOptions);
     var directionsDisplay=[];
     for(var i=0;i<myObj2.length-1;i++){//myObj2.length=3
     var depart = new google.maps.LatLng(myObj2[i].latitude, myObj2[i].longitude);// for :i=0 = point(A) .... i=1 =point(B)
    var destination = new google.maps.LatLng(myObj2[i+1].latitude, myObj2[i+1].longitude);//i=1 : point(B)...i=2 =point(C)
     directionsService = new google.maps.DirectionsService;
     directionsDisplay[i] = new google.maps.DirectionsRenderer;
      directionsDisplay[i].setMap(map);
      var request = {
        origin: depart,
        destination: destination,
        travelMode: google.maps.DirectionsTravelMode.DRIVING
       };
      directionsService.route(request, function(response, status) {
      if (status == google.maps.DirectionsStatus.OK) {
       alert(i) ; // I see that it does not display i = 0 nor i = 1 .... it shows me only i = 2 whereas normally the for-stop loop on i <2 (i = 1)
          directionsDisplay[i].setDirections(response);
        }
      });
    }
    }
    };
    xmlhttp.open("GET", "db4.php?datee="+dd1+"&voiture="+dd6, true);
    xmlhttp.send();

我使用一个for循环,如果我用警报(i)进行测试,我发现它不显示i = 0或i = 1 ....它仅显示i = 2 而通常i <2(i = 1)

上的for-stop循环

0 个答案:

没有答案