首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Google Map api v3中使用交通工具作为我的出行方式

在Google Map api v3中使用交通工具作为我的出行方式
EN

Stack Overflow用户
提问于 2012-10-27 17:09:08
回答 2查看 5K关注 0票数 1

当我在Google Map api V3中使用运输作为我的出行模式时,我在DirectionsRequest中定义了起点、目的地和一些路点。然而,当DirectionsResult回来时,DirectionsLeg只从我的起点开始,以我的目的地结束,它跳过了我所有的航点。我的代码如下所示,这里有没有人遇到同样的问题?

代码语言:javascript
复制
function calcRoute(waypts, mode) {
var sites = [];
var mode;

//Add waypoints to array, the first and last one are not added in waypoints
for (var i = 1; i < waypts.length-2; i++) {
    sites.push({
        location:waypts[i],
        stopover:true}); //Set true to show that stop is required
}

var request = {
    origin: waypts[0], //Set the first one as origin
    destination:waypts[waypts.length-1],//Set the last one as destination
    waypoints:sites,//Set waypoints
    optimizeWaypoints:false,
    travelMode: google.maps.TravelMode[mode]
};

    //Send to Google
directionsService.route(request, function(response, status) {
  if (status == google.maps.DirectionsStatus.OK) {
    directionsDisplay.setDirections(response);
    var route = response.routes[0];
    var HTMLContent = "";

            //Get response and show on my HTML
    for(var i =0; i < route.legs.length; i++){
        HTMLContent = "From " + route.legs[i].start_address + "To " + route.legs[i].end_address + "<br>";
        HTMLContent =  HTMLContent + "Distance:" + route.legs[i].distance.text + "<br>";
    }       
    $("#route_Scroll").append(HTMLContent);
  }else{
      alert(status);
  }
});

}

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-10-28 21:49:39

是啊,https://developers.google.com/maps/documentation/javascript/directions#TransitOptions

“方向请求的可用选项因出行模式而异。请求交通方向时,将忽略avoidHighways、avoidTolls、waypoints[]和optimizeWaypoints选项。您可以通过TransitOptions对象文字指定特定于交通的路由选项。”

如果你想使用它,你必须拆分请求。

票数 1
EN

Stack Overflow用户

发布于 2012-10-27 22:09:14

当TravelMode正在传输时,您不能指定路点。

the documentation (now) states:

运输方向不支持

路点。

在这种情况下,directions服务总是返回INVALID_REQUEST。

Example

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13098737

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档