user1779012
user1779012

Reputation: 114

Using TRANSIT as my travel mode in Google Map api v3

When I was using TRANSIT as my travel mode in Google Map api V3, I defined the origin, destination and some waypoints in DirectionsRequest. However when DirectionsResult came back, DirectionsLeg only started with my origin and ended with my destination, it skipped all my waypoints. My codes are shown as below Does anyone get the same problem here?

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

}

Upvotes: 1

Views: 5818

Answers (2)

geocodezip
geocodezip

Reputation: 161334

You can't specify waypoints when TravelMode is TRANSIT.

the documentation (now) states:

Waypoints are not supported for transit directions.

The directions service always returns INVALID_REQUEST in that case.

Example

Upvotes: 1

kaskader
kaskader

Reputation: 1972

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

"The available options for a directions request vary between travel modes. When requesting transit directions, the avoidHighways, avoidTolls, waypoints[] and optimizeWaypoints options will be ignored. You can specify transit specific routing options through the TransitOptions object literal."

If you want to use it you would have to split the request.

Upvotes: 1

Related Questions