Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I draw a railway route on Google Maps?
    primarykey
    data
    text
    <p>This is my code:</p> <pre class="lang-js prettyprint-override"><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 &lt; legs.length; i++) { var steps = legs[i].steps; for (j = 0; j &lt; steps.length; j++) { var nextSegment = steps[j].path; for (k = 0; k &lt; nextSegment.length; k++) { polyline.getPath().push(nextSegment[k]); //bounds.extend(nextSegment[k]); } } } } }); } </code></pre> <p>Using <code>travelMode: google.maps.DirectionsTravelMode.WALKING</code> I see the line, but it is not what I'm looking for. I think I need <code>.TRANSIT</code> with some more options, like <code>VehicleType.RAIL</code>.</p> <p>I don't know how to do it! I've tried on searching on Google, but couldn't find anything!</p> <hr> <p>I think this is the right way to do this :</p> <pre><code>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 &lt; legs.length; i++) { var steps = legs[i].steps; for (j = 0; j &lt; 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 &lt; nextSegment.length; k++) { polyline.getPath().push(nextSegment[k]); } } } } } } }); </code></pre> <p>but : </p> <p>1) why it draw only a part of route?</p> <p>2) on the returned JSON object of DirectionsStatus, there are also info about walking and/or TRANSIT. So what's the meaning of <code>travelMode: google.maps.DirectionsTravelMode.TRANSIT</code> ?!?!?</p> <p>but I get <code>TypeError: google.maps.TransitVehicle is undefined</code></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload