Google maps 在Google Maps v3中绘制超过8个航路点

Google maps 在Google Maps v3中绘制超过8个航路点,google-maps,google-maps-api-3,Google Maps,Google Maps Api 3,将代码从Javascript API 2迁移到3。我有一个位置列表,需要以驾驶方向图的形式绘制。这是在v2中使用以下代码完成的 directions = new GDirections(map); directions.loadFromWaypoints(waypoints, {preserveViewport: true}); 下面是我将其转换为V3的尝试 var request = { origin: startLoc, destination: endLoc,

将代码从Javascript API 2迁移到3。我有一个位置列表,需要以驾驶方向图的形式绘制。这是在v2中使用以下代码完成的

directions = new GDirections(map);
directions.loadFromWaypoints(waypoints, {preserveViewport: true});  
下面是我将其转换为V3的尝试

var request = {
    origin: startLoc,
    destination: endLoc,
    waypoints: waypoints,
    optimizeWaypoints: true,
    travelMode: google.maps.TravelMode.DRIVING
};          

directionsService.route(request, function(response, status) {
    if (status == google.maps.DirectionsStatus.OK) {
        directionsDisplay.setDirections(response);
    } 
});
不是全部代码,而是总体思路。看起来很好,只是有点小问题。当超过8个航路点时,呼叫失败。这是预期的,因为

允许的最大航路点为8,加上起点和目的地。业务客户的Maps API允许23个航路点,加上起点和目的地。过境方向不支持航路点

因为我在v2中没有遇到这个问题,这是v3的新限制吗?我想知道我是不是在使用一些不是为我所需要的东西而设计的东西。这是一个使用量很小的应用程序,有2个用户,所以我不确定昂贵的商业许可证是否值得返还。发给谷歌地图团队的电子邮件尚未回复。任何解决方法/指针都会大有帮助。谢谢。

一个可能的解决方法(特别是对于使用较少的站点)是使用多个方向服务请求


    • 使用应该需要使用如下数组概念

      directionsService[i].route({                                    
                          'origin': start,
                          'destination': end
                          'waypoints': waypts,
                          'travelMode': 'DRIVING'
                             },
                          function (directions, status){              
                                      if (status == google.maps.DirectionsStatus.OK) {
                                          directionsDisplay[j].setDirections(directions);
                                      }
                                  });
      
      在这些directionservice和directiondisplay中,都应该在数组逻辑中 使用循环概念时,应动态分配起点、终点和航路点,

      尝试发送多个请求意味着您将获得n个latlon的路由…但是标记将在8个航路点后以相同的名称重复,因此我们使用SupersMarkers false属性删除默认标记…

      正如其他人所说,使用Google的JavaScript API是不可能做到的。然而,谷歌允许一个服务器端请求最多23个航路点。因此,使用PHP和JavaScript,我能够找到一个解决方法

      您需要做的是从服务器端请求中获取“航路点顺序”,然后让JavaScript为您提供每个位置之间的方向

      <?php
        $startLocation = "40.713,-74.0135";
        $endLocation = "40.75773,-73.985708";
        $waypoints = array("40.748433,-73.985656|", "40.689167,-74.044444|");
        $apiKey = "";
        $routeData = json_decode(file_get_contents("https://maps.googleapis.com/maps/api/directions/json?origin=".$startLoc."&destination=".$endLoc."&waypoints=optimize:true|".$waypoints."&key=".$apiKey));
        echo "var waypointsOrder = ". json_encode($routeData->routes[0]->waypoint_order) . ";\n";
      ?>
      
      var startLocation = {lat: 40.713, lng: -74.0135};
      var endLocation = {lat: 40.75773, lng: -73.985708};
      
      //get directions from the origin to the first waypoint
      var request = {
          origin: startLocation,
          destination: JSON.parse(places[waypointsOrder[0]][1]),
          travelMode: google.maps.TravelMode.DRIVING
      };
      directionsService.route(request, function (result, status) {
          if (status == google.maps.DirectionsStatus.OK) {
                renderDirections(result, map);
          }
      });
      
      //get directions from the last waypoint to the destination
      var request = {
          origin: JSON.parse(places[waypointsOrder[waypointsOrder.length-1]][1]),
          destination: endLocation,
          travelMode: google.maps.TravelMode.DRIVING
      };
      directionsService.route(request, function (result, status) {
          if (status == google.maps.DirectionsStatus.OK) {
                renderDirections(result, map);
          }
      });
      
      //get directions between each waypoint
      for (i = 1; i < waypointsOrder.length; i++) {
        var request = {
            origin: JSON.parse(places[waypointsOrder[i-1]][1]),
            destination: JSON.parse(places[waypointsOrder[i]][1]),
            travelMode: google.maps.TravelMode.DRIVING
        };
        directionsService.route(request, function (result, status) {
            if (status == google.maps.DirectionsStatus.OK) {
                renderDirections(result, map);
            }
        });
      }
      
      function renderDirections(result, map) {
        var directionsDisplay = new google.maps.DirectionsRenderer ({
          map: map
        });
        directionsDisplay.setMap(map); 
        directionsDisplay.setDirections(result); 
      }
      
      
      var=纬度:40.713,液化天然气:-74.0135};
      var endLocation={lat:40.75773,lng:-73.985708};
      //获取从原点到第一个航路点的方向
      var请求={
      起源:惊吓,
      destination:JSON.parse(places[waypointsOrder[0]][1]),
      travelMode:google.maps.travelMode.DRIVING
      };
      路由(请求、功能(结果、状态){
      if(status==google.maps.directionstatus.OK){
      渲染方向(结果、地图);
      }
      });
      //获取从最后一个航路点到目的地的方向
      var请求={
      来源:JSON.parse(places[waypointsOrder[waypointsOrder.length-1]][1]),
      目的地:endLocation,
      travelMode:google.maps.travelMode.DRIVING
      };
      路由(请求、功能(结果、状态){
      if(status==google.maps.directionstatus.OK){
      渲染方向(结果、地图);
      }
      });
      //获取每个航路点之间的方向
      对于(i=1;i
      v2有25个航路点的限制。在v3中,限制降低到了8。这个链接指向一个类似的问题和答案——我阅读了“地图API”中的商业成本$10000/年,尽管我在任何谷歌网站上都没有看到。如果这是正确的,你有什么想法吗?只是看看雅虎,他们似乎已经关闭了地图API服务。看起来很有希望,尽管我不太明白多批次结果是如何重新排序的。将尝试编码并让您知道。谢谢。对不起,没有注意到航路点。那可能是个问题。有用,我没有意识到你可以提出多个方向的服务请求,出于某种原因,我认为如果你需要8个以上的航路点,你只能每页做一个,并且你需要对它们进行优化,你需要1个。购买“商业地图”许可证(最多允许23个航路点)或2个。在使用这种绘制方向的方法之前,请自己优化航路点。你也许可以使用来优化航路点。我觉得奇怪的是,谷歌将允许你使用的航路点数量从V2降低到V3。营业执照费用高达每年1万美元。我刚刚从应用程序中删除了该功能。这不值得付出额外的代价。