这是我的代码:
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]);
}
}
}
}
});
}
使用travelMode: google.maps.DirectionsTravelMode.WALKING
I see the line,但这不是我想要的。我想我需要.TRANSIT
更多的选择,比如VehicleType.RAIL
.
我不知道该怎么做!我试过在谷歌上搜索,但找不到任何东西!
我认为这是正确的方法:
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]);
}
}
}
}
}
}
});
但 :
1)为什么它只画了一部分路线?
2) 在 DirectionsStatus 返回的 JSON 对象上,还有关于步行和/或 TRANSIT 的信息。那么travelMode: google.maps.DirectionsTravelMode.TRANSIT
?!?! 是什么意思?
但我明白了TypeError: google.maps.TransitVehicle is undefined