在Google Map api v3中使用TRANSIT作为我的旅行模式

时间:2012-10-27 09:09:08

标签: google-maps google-maps-api-3

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

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);
  }
});

}

2 个答案:

答案 0 :(得分:1)

当TravelMode为TRANSIT时,您无法指定航点。

the documentation (now) states:

  

过境路线不支持航点。

在这种情况下,路线服务始终会返回INVALID_REQUEST。

Example

答案 1 :(得分:1)

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

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

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