我有这个功能可以输出特定路线期间的总海拔增益和下降。我还想导出该段的平均坡度(即斜率)。我是否使用方程 m = 上升/运行?我将如何实施呢?
var writeStats = function(r) {
var prevElevation = r[0].elevation;
var climb = 0;
var drop = 0;
var max = 0;
for (var i = 1; i < r.length; i++) {
var diff = r[i].elevation - prevElevation;
prevElevation = r[i].elevation;
if (diff > 0) {
climb += diff;
climb = Math.abs(climb / 1) ;//Object to number
var climb2 = climb * 3.2808399;//Metres to feet
}
else {
drop -= diff;
drop = Math.abs(drop / 1) ;
var drop2 = drop * 3.2808399;
}
if (r[i].elevation > max) {
max = r[i].elevation;
}
}
max = Math.ceil(max);
$('#climb-drop').text("Climb: " + Math.round(climb2) + " feet Drop: " + Math.round(drop2) + " feet");
return max;
};
更新:我想将其与计算路线的函数联系起来:
var from = new google.maps.LatLng(40.750484, -111.811177);
var to = new google.maps.LatLng(40.775041, -111.718560);
ds.route({
origin: from,
destination: to,
travelMode: google.maps.DirectionsTravelMode.DRIVING,
unitSystem: google.maps.UnitSystem.IMPERIAL
}, function(result, status) {
if (status == google.maps.DirectionsStatus.OK) {
fitBounds = true;
dr.setDirections(result);
}
});