【问题标题】:Calculate My Speed With Geolocation API javascript使用 Geolocation API javascript 计算我的速度
【发布时间】:2015-10-06 00:30:00
【问题描述】:

可以通过谷歌地图 javascript for android 的地理位置计算移动设备的移动速度吗?

【问题讨论】:

    标签: javascript android performance cordova geolocation


    【解决方案1】:

    至少如果您使用Geolocation plugin 提供的本地地理定位服务,您可以获得足够准确的位置,您可以从中计算速度

    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);
    

    其中 NumbertoRad 函数来自Caspar Kleijne's answer,两个坐标之间的距离计算来自cletus' answert2 和 t1 为单位,纬度(lat1 和 lat2)和经度(lng1 和 lng2)是浮点数。

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

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

    【讨论】:

    • 那么,您发布的代码到底是做什么的?只计算速度,还是距离?
    • 请阅读 cletus 的回答,了解如何计算两个位置之间的距离(以坐标表示)。在我们知道距离 d 后,我们通过公式 v = d/(t2 - t1)得到速度(或通常也称为速度)v > 其中 t2t1 是进行位置测量的时间点。当我们执行 t2 - t1 时,我们得到了用于行进距离的时间,这又是速度的定义。有关如何计算速度的更多信息,请参阅Wikipedia
    • 感谢 geolocation cordova 插件,我现在有了我的坐标,但是现在我如何在'cletus'的脚本中传递它们? .我有'纬度''经度',我没有变量。
    • @nonenane:添加了更具体的示例。虽然没有测试它。你应该可以在那里看到这个想法。
    • 我不明白如何将我的坐标放入您发布的脚本中。还有一件事,我如何找到第二个点,然后计算距离?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-28
    • 2013-07-08
    相关资源
    最近更新 更多