This is my code:
function trainRoute(startPoint, endPoint) {
var polyline = new google.maps.Polyline({ strokeColor: '#af1a1a', strokeWeight: 2 });
var directionsService = new google.maps.DirectionsService();
var directionsDisplay = new google.maps.DirectionsRenderer({ polylineOptions: polyline, suppressMarkers: true });
directionsDisplay.setMap(map);
var request = {
origin: startPoint,
destination: endPoint,
travelMode: google.maps.DirectionsTravelMode.TRANSIT
};
directionsService.route(request, function (response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
var legs = response.routes[0].legs;
for (i = 0; i < legs.length; i++) {
var steps = legs[i].steps;
for (j = 0; j < steps.length; j++) {
var nextSegment = steps[j].path;
for (k = 0; k < nextSegment.length; k++) {
polyline.getPath().push(nextSegment[k]);
//bounds.extend(nextSegment[k]);
}
}
}
}
});
}
Using travelMode: google.maps.DirectionsTravelMode.WALKING I see the line, but it is not what I’m looking for. I think I need .TRANSIT with some more options, like VehicleType.RAIL.
I don’t know how to do it! I’ve tried on searching on Google, but couldn’t find anything!
I think this is the right way to do this :
var request = {
origin: puntoPartenza,
destination: puntoArrivo,
travelMode: google.maps.TravelMode.TRANSIT
};
directionsService.route(request, function (response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
var legs = response.routes[0].legs;
for (i = 0; i < legs.length; i++) {
var steps = legs[i].steps;
for (j = 0; j < steps.length; j++) {
var transitMode = steps[j].travel_mode;
if (transitMode == "TRANSIT") {
var vehicle = steps[j].transit.line.vehicle.type;
if (vehicle == "HEAVY_RAIL") {
var nextSegment = steps[j].path;
for (k = 0; k < nextSegment.length; k++) {
polyline.getPath().push(nextSegment[k]);
}
}
}
}
}
}
});
but :
1) why it draw only a part of route?
2) on the returned JSON object of DirectionsStatus, there are also info about walking and/or TRANSIT. So what’s the meaning of travelMode: google.maps.DirectionsTravelMode.TRANSIT ?!?!?
but I get TypeError: google.maps.TransitVehicle is undefined
You can’t just make up syntax and expect it to work Transit VehicleType does include RAIL, but TransitVehicle, doesn’t have a vehicle property.
There is no property vehicle in Transit vehicle. Look at the “type” property.