使用googlegeolocation数组中的坐标和从开始到结束的持续时间来计算速度

using google geolocation array of coordinates and duration from start to end to calculate the pace

本文关键字:持续时间 结束 速度 计算 开始 数组 googlegeolocation 坐标 使用      更新时间:2023-09-26

我有一个健身应用程序,它可以跟踪跑步者,并在最后显示跑步的距离和时间,并使用谷歌地图和地理定位api在地图上画一条线。我需要根据这些参数来计算速度。

假设跑步的总距离是7公里,跑步的持续时间是37分钟,那么计算速度的正确方法是什么,速度应该是正确的,并以分钟为单位显示:秒形成,而不是5.281。。这就是我现在得到的:

function get_Runtime() { //this function takes the duration and convert to total of seconds
    var sec = parseInt(document.getElementById("seconds").innerHTML)
    var minute = parseInt((document.getElementById("minutes").innerHTML) * 60)
    var total_time = (sec + minute);
    return total_time;
}
function average_pace(distance) { // this function takes duration and devide by distance
    var avgnum = get_Runtime() / distance
    return avgnum.toPrecision(3);
}

结果总是像2.322,但我需要的是将其转换为分钟:像2:34这样的秒谢谢你的帮助。

要得到分钟除以秒的60。要获得分钟:秒,请执行

parseInt(seconds/60) + ':' + seconds%60

这将输出类似于:

avgnum = 567;
var minutes = parseInt(avgnum/60) //9;
var seconds = avgnum%60 //27;
var formatted = minutes + ':' + seconds;

我使用的运算符叫做modulo。这需要除法的余数,因此7%3 = 18%4 = 0

parseInt取十进制的下限值。