使用地理位置API javascript计算我的速度

使用地理位置API javascript计算我的速度,javascript,android,performance,cordova,geolocation,Javascript,Android,Performance,Cordova,Geolocation,可以计算移动设备在Google Maps javascript for android的地理位置中移动的速度?至少如果您使用由您提供的本机地理位置服务,可以获得足够精确的位置,从而可以根据需要计算速度 function calculateSpeed(t1, lat1, lng1, t2, lat2, lng2) { // From Caspar Kleijne's answer starts /** Converts numeric degrees to radians */ if

可以计算移动设备在Google Maps javascript for android的地理位置中移动的速度?

至少如果您使用由您提供的本机地理位置服务,可以获得足够精确的位置,从而可以根据需要计算速度

function calculateSpeed(t1, lat1, lng1, t2, lat2, lng2) {
  // From Caspar Kleijne's answer starts
  /** Converts numeric degrees to radians */
  if (typeof(Number.prototype.toRad) === "undefined") {
    Number.prototype.toRad = function() {
      return this * Math.PI / 180;
    }
  }
  // From Caspar Kleijne's answer ends
  // From cletus' answer starts
  var R = 6371; // km
  var dLat = (lat2-lat1).toRad();
  var dLon = (lon2-lon1).toRad();
  var lat1 = lat1.toRad();
  var lat2 = lat2.toRad();

  var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
    Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) *    Math.cos(lat2); 
  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
  var distance = R * c;
  // From cletus' answer ends

  return distance / t2 - t1;
}

function firstGeolocationSuccess(position1) {
  var t1 = Date.now();
  navigator.geolocation.getCurrentPosition(
    function (position2) {
      var speed = calculateSpeed(t1 / 1000, position1.coords.latitude, position1.coords.longitude, Date.now() / 1000, position2.coords.latitude, position2.coords.longitude);
    }
}
navigator.geolocation.getCurrentPosition(firstGeolocationSuccess);
当数字的toRad函数为from时,两个坐标之间的距离计算为from,t2和t1以秒为单位,纬度(lat1和lat2)和经度(lng1和lng2)为浮点数

代码的主要思想如下: 1.获取初始位置并在该位置存储时间, 2.获取另一个位置,获取后,使用位置和时间调用calculateSpeed函数


同样的公式当然适用于谷歌地图案例,但在这种情况下,我会检查计算的准确性,因为即使是网络延迟也可能会导致一些测量错误,如果时间间隔太短,这些错误很容易成倍增加。

那么,您发布的代码到底是什么?仅计算速度或距离?请阅读cletus关于如何计算两个位置之间距离(以坐标表示)的回答。在我们知道距离d之后,我们用公式v=d/(t2-t1)得到速度(或通常称为速度)v,其中t2和t1是测量位置时的时间点。当我们做t2-t1时,我们得到了移动距离的时间,这也是速度的定义。有关如何计算速度的更多信息,请参阅。由于地理位置cordova插件,我现在有了坐标,但现在我如何在“cletus”的scritp中传递它们。我有“纬度”和“经度”,我没有变量。@nonenane:添加了更具体的示例。但没有测试它。你应该可以在那里看到这个想法。我不明白如何在你发布的脚本中输入我的坐标。还有一件事,我如何找到第二个点,然后计算距离?