与此功能类似,计算驾驶方向模式中两点之间的距离:
function calcDistance(p1, p2) {
return (google.maps.geometry.spherical.computeDistanceBetween(p1, p2) / 1000).toFixed(2);
}
如何计算步行方向模式中两点之间的距离?
答案 0 :(得分:0)
使用方向服务的计算方法与驾驶,步行,骑车相同。这是一个基本的例子:
directionsService.route(request, function(result, status) {
if (status == google.maps.DirectionsStatus.OK) {
path = result.routes[0].overview_path;
var sumKM = 0;
var myroute = result.routes[0];
for (var i = 0; i < myroute.legs.length; i++) {
sumKM += myroute.legs[i].distance.value; //legs = Abschnitte from point to point
}
sumKM = sumKM / 1000;
//console.log("Route calculated, write the route")
//writeRoute(path, sumKM, index)
} else if (status == google.maps.DirectionsStatus.ZERO_RESULTS) {
alert("Could not find a route between these points");
} else {
alert("Directions request failed! Reset Marker!");
}
});