我知道使用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);
}
});