我知道使用 Google API,可以计算两点之间的路线距离,但是我看到的大多数示例是方向服务计算汽车/步行/自行车路线上的距离。
从代码中可以得到两个大城市在铁路上的距离(代码如下),但我也想得到 16 个段的距离,它们是两个大城市之间的小站。如何?
var directionsService = new google.maps.DirectionsService();
var directionsDisplay = new google.maps.DirectionsRenderer(rendererOptions);
directionsDisplay.setMap(map);
var request = {
origin: 'Chester',
destination: 'Liverpool',
travelMode: google.maps.TravelMode.TRANSIT,
unitSystem:google.maps.UnitSystem.METRIC
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
////////////////////////////////////////////////////////////////////////////
directionsDisplay.setDirections(response);
var path = response.routes[0].overview_path;
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;
console.log('segment '+nextSegment.length);
for (k=0;k<nextSegment.length;k++) {
console.log(nextSegment[k].hb+" "+nextSegment[k].ib);
route_polyline.getPath().push(nextSegment[k]);
}
console.log('rail');
}
}
}
}
route_polyline.setMap(map);
}
});