google的航点限制解决方法,无法获取路线

时间:2019-05-10 16:14:18

标签: google-maps

我一直在研究这段代码,以显示路由算法的输出,

所以我必须传递一个数组数组,每个数组都包含特定车辆的完整路线。

问题在于路线可能太长,这意味着我将撞到Google的25个航路点障碍。

所以我尝试将每个数组切成较小的部分,并使用其他代码段连接道路。

现在代码对我来说似乎是正确的,但是我不断收到路由零结果错误,或者未传递数组。

我假设此处的最大航路点限制为4,因为我不想在试验phasse中通过完整的路线数组。

现在,当我尝试相同的代码来检查python上是否有空数组时,我什么也没得到。

但是当我尝试使用google时,它总是说同样的错误

     function calculateRoute(from, to) {
    //use only a single maps-instance
    if(!window.mapObject){
         var myOptions = {
           zoom: 5,
               center: new google.maps.LatLng(52.760930399999999, 4.8886201000000264),
           mapTypeId: google.maps.MapTypeId.ROADMAP
         };
     // Draw the map
         mapObject = new google.maps.Map(document.getElementById("map"), myOptions);
     // you also need only a single DirectionsService-instance
     directionsService = new google.maps.DirectionsService();
 }
 //array of arrrays containing the route of each vehicle
 var wpoints = [[[52.760930399999999, 4.8886201000000264], [52.739666299999996, 4.8571595999999317], [52.775776499999999, 4.8089896000000181], [52.8188265, 4.8723703000000569], [52.795917700000004, 4.8037706999999727], [52.736762300000002, 4.8508724000000711], [52.736762300000002, 4.8508724000000711], [52.785122999999999, 4.8049109999999473], [52.785122999999999, 4.8049109999999473], [52.914619900000005, 4.969052099999999], [52.949379899999997, 4.7604910999999683], [52.862199699999998, 4.8310553999999675], [52.775776499999999, 4.8089896000000181], [52.775776499999999, 4.8089896000000181], [52.739666299999996, 4.8571595999999317], [52.722574800000004, 4.8887746000000334], [52.786345300000001, 4.8694027000000233], [52.8188265, 4.8723703000000569], [52.775776499999999, 4.8089896000000181], [52.862975600000013, 4.8221360999999661], [52.864006499999995, 4.8292355999999472], [52.782838200000008, 4.7832250000000167], [52.782838200000008, 4.7832250000000167], [52.864006499999995, 4.8292355999999472]], [[52.760930399999999, 4.8886201000000264], [52.845149300000003, 4.9698283000000165], [52.746306700000005, 4.907192699999996], [52.746278000000004, 4.9071639999999661], [52.647193799999997, 5.054055500000004], [52.648299899999991, 5.0517600000000584], [52.855798000000007, 5.0267980999999509], [52.773210399999996, 5.1031308000000308], [52.852283000000007, 5.0354371999999339], [52.897665400000001, 5.069036800000049], [52.741116599999998, 5.0572813999999644], [52.741116599999998, 5.0572813999999644], [52.897665400000001, 5.069036800000049], [52.644786700000012, 5.0547002000000703], [52.928940599999997, 4.9823238000000165]]];
 var parts = [[]];
 var color_var = 0;
 var colorz = ['red','black'];
 var max = 4;// i assumed the max number was 4 as a trial because i didn't want to pass the full route in the trial phase
 //the slicing part

 //push each route's waypoints into its own array 
 for (var j = 0; j < wpoints.length; ++j){
        i=0;
       while(i < wpoints[j].length){
            parts[j].push(wpoints[j].slice(i,i + max + 1));
            i = i+max;
      }      
      parts.push([]);
 }
 parts = parts.slice(0,-1); //get rid of the last empty array

 for (var k = 0; k < wpoints.length; ++k){//iterate through each route array 
    for (var i = 0; i < parts[k].length; ++i){// iterate through each waypoint array
       waypts = [];
       for (var j = 0; j < parts[k][i].length-1; ++j){
           waypts.push({
                 location: new google.maps.LatLng(parts[k][i][j][0],parts[k][i][j][1]),
                 stopover: true
                   });
       }

     var directionsRequest = {
     origin: new google.maps.LatLng(parts[k][i][0]),
     destination: new google.maps.LatLng(parts[k][i][parts[k][i].length - 1]),
     waypoints: waypts,
     optimizeWaypoints: true,
     travelMode: google.maps.DirectionsTravelMode.DRIVING,
     unitSystem: google.maps.UnitSystem.METRIC
                   };
                   //alert(i);
     directionsService.route(
       directionsRequest,
       function(response, status)
       { 
     if (status == google.maps.DirectionsStatus.OK)
     {
       new google.maps.DirectionsRenderer({
         map: mapObject,
         //suppressInfoWindows: true,
         suppressMarkers: true,
             directions: response,
         polylineOptions: {
                       strokeColor: colorz[color_var]
                     }                 
                   });
     }
     else
       window.alert("Unable to retrieve your route, error is "+ status);
       }
    );
} ++color_var;}

我需要的是所有路线在屏幕上的路线。

0 个答案:

没有答案