【问题标题】:React Native Geolocation average speedReact Native Geolocation 平均速度
【发布时间】:2017-03-07 08:06:47
【问题描述】:

我有一个 react native 应用程序,我可以在其中跟踪用户的位置并在地图上显示他的移动。我的目标是显示用户的平均旅行速度。

现在在 React Native 的地理位置 watchPosition 的响应是一个速度参数,这只是从最后一个已知坐标到新坐标。

回复:

{
  coords: {
    accuracy: 5,
    altitude: 0,
    altitudeAccuracy: -1,
    heading: 108.43,
    latitude: 38.333034435,
    longitude: -122.04128193
    speed: 6.5
  }
  timestamp: 1465109890871.304
}

有没有办法获得整个轨迹的平均速度?或者我该如何计算这个速度?

将所有“速度”值保存在一个数组中,然后将其除以数组中值的数量,这听起来很荒谬,而且性能不佳。

非常感谢任何帮助!

非常感谢!

【问题讨论】:

  • 我按照你解释的方式做了,存储在一个数组中,然后除以数组中的数量。你知道速度是什么指标吗? (公里或米)

标签: javascript reactjs react-native geolocation


【解决方案1】:

我使用 getCurrentPositionreduxthis 公式的方式 -

  var speedCounter = 0;

  setInterval(() => {
    navigator.geolocation.getCurrentPosition(success, error, options);
  }, 1000);

  function success(position) {
    ++speedCounter;
    var speed = position.coords.speed < 0 ? 0 : Math.round(position.coords.speed);
    var topSpeed = getState().data.topSpeed;
    var avgSpeed = getState().data.avgSpeed;

    dispatch({
      type: DATA_SPEED,
      speed: speed,
      topSpeed: speed > topSpeed ? speed : topSpeed,
      avgSpeed: Math.round((avgSpeed * (speedCounter - 1) + speed) / speedCounter),
    });
  }

我使用getCurrentPosition 是因为watchPosition 对我来说效果不佳。但那是很久以前的事了。不确定watchPosition 在最新版本中的准确度。

【讨论】:

    【解决方案2】:

    为@vinayr 提供另一种解决方案 如果精度不够好,您可能有 GPS 纬度/经度,但没有速度(在我的情况下,我有 speed=-2),因此您可能需要的不仅仅是速度。

    每当您获得一些新坐标时,您都会使用来自 https://stackoverflow.com/a/23448821/1611358 或此 npm 模块 https://www.npmjs.com/package/haversine 的 calculateDistance 方法计算到先前位置的距离。

    那么你有 2 个选项来计算平均速度:

    • 当速度为负时,您计算它(使用上述方法 + 两个坐标之间的持续时间:使用时间戳),然后使用 nb 个度量值 + 上次平均速度 + 新位置速度更新您的平均速度(如通过@vinayr)

    • 你在两个变量totalDistance和totalDuration中附加距离和持续时间,你的平均速度总是totalDistance/totalDuration(我在做什么)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多