【问题标题】:Using TRANSIT as my travel mode in Google Map api v3在 Google Map api v3 中使用 TRANSIT 作为我的旅行模式
【发布时间】:2012-10-27 09:09:08
【问题描述】:

当我在 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);
  }
});

}

【问题讨论】:

  • 您能否提供一个显示问题的示例起点、终点和航路点?
  • 还有您正在使用的 TransitOptions。

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


【解决方案1】:

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

“路线请求的可用选项因出行模式而异。请求公交路线时,将忽略 AvoidHighways、AvoidTolls、Waypoints[] 和 optimizeWaypoints 选项。您可以通过 TransitOptions 对象文字指定公交特定的路线选项。”

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

【讨论】:

    【解决方案2】:

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

    the documentation (now) states:

    公交路线不支持航点。

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

    Example

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-22
      • 1970-01-01
      • 1970-01-01
      • 2013-07-18
      • 1970-01-01
      相关资源
      最近更新 更多