从2点计算速度

发布于 2024-11-07 20:11:47 字数 739 浏览 0 评论 0原文

我知道这个问题已经被问了很多次,但还没有让我满意。我正在尝试使用 Android 设备的 GPS 来计算速度。很多人似乎回答说只需使用 Location 对象的 getSpeed() 函数即可。据我了解,getSpeed() 仅适用于 GPS 接收器芯片中内置有速度传感器的某些设备。不管怎样,我希望我的应用程序能够正常工作,因此我使用以下半正矢公式:

private double CalculateHaversineMI(double lat1, double long1, double lat2,double long2) {
    double dlong = (long2 - long1) * (Math.PI / 180.0f);
    double dlat = (lat2 - lat1) * (Math.PI / 180.0f);
    double a = Math.pow(Math.sin(dlat / 2.0), 2)
        + Math.cos(lat1 * (Math.PI / 180.0f))
        * Math.cos(lat2 * (Math.PI / 180.0f))
        * Math.pow(Math.sin(dlong / 2.0), 2);
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    double d = 3956 * c;

    return d;
}

现在我想做的是弄清楚如何从中计算速度。有人可以帮我吗?

I know this question has been asked a lot, but not yet to my satisfaction. I am trying to use the GPS of an Android device to calculate speed. A lot of people seem to reply by saying to simply use the getSpeed() function of the Location object. From what I understand though, getSpeed() will only work on certain devices that have a speed sensor built into the GPS receiver chip. I want my application to work regardless of this, so I am using the following haversine formula:

private double CalculateHaversineMI(double lat1, double long1, double lat2,double long2) {
    double dlong = (long2 - long1) * (Math.PI / 180.0f);
    double dlat = (lat2 - lat1) * (Math.PI / 180.0f);
    double a = Math.pow(Math.sin(dlat / 2.0), 2)
        + Math.cos(lat1 * (Math.PI / 180.0f))
        * Math.cos(lat2 * (Math.PI / 180.0f))
        * Math.pow(Math.sin(dlong / 2.0), 2);
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    double d = 3956 * c;

    return d;
}

Now what I'm trying to do is figure out how to calculate speed from this. Can anyone help me please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最单纯的乌龟 2024-11-14 20:11:47

我可以看到你的函数返回球体上两点之间的正确路径距离 - 它是d。但是,只有当球体上有 2 个彼此不靠近的点时才需要这个公式(意味着它们分开的圆心角不小,1 度的圆心角对应于大约 111 公里的距离,只是为了感觉一下)。如果它们彼此靠近(对于移动的人和低速车辆来说就是这种情况),那么您不需要这个公式。你可以简单且非常精确地用直线近似球体上的弧,然后计算变得微不足道。

  • 定期采样 GPS 位置。计算距最后获得的位置的距离。为此,您可以使用 android.location.Location 中的 distanceTo() 函数。
  • 通过将距离除以两次测量之间经过的时间来计算速度。
  • 平均计算速度以获得更准确的结果,但确保您不会失去对速度变化的敏感性。因此,您需要对平均样本数量进行一些权衡。

What I can see is that your function returns correct path distance between 2 points on a sphere - it is d. But, this formula is needed only if you have 2 points on a sphere that are not close to each other (means central angle of their separation is not small, central angle of 1 degree corresponds to distance of 111 km approx, just to get feeling). If they are close to each other (which is the case for people moving and slow speed vehicles), then you do not need this formula. You can simply and very accurately approximate arc on the sphere with the straight line, and then calculation becomes trivial.

  • Sample GPS position at regular time periods. Calculate distance from the last position obtained. For that purpose you may use distanceTo() function from android.location.Location.
  • Calculate speed by dividing distance with time elapsed between 2 measurements.
  • Average calculated speeds for more accurate results, but ensure that you do not lose sensitivity to speed changes. So, you would need some trade-off on number of samples averaged.
下壹個目標 2024-11-14 20:11:47

这样就可以计算出距离了。现在您可能还记得,速度 = 距离/时间,因此在沿线的某个位置您需要捕获时间和位置。

另一方面,您使用的公式对于您想要做的事情来说是OTT。基于您所经过的路径远小于地球周长这一事实,您最好进行一些近似。然后你可以得到一个更简单的公式。

That calculates the distance. Now as you may recall, speed = distance / time, so somewhere along the line you need to capture the time as well as the position.

On another note, the formula that you are using is way OTT for what you are trying to do. You would be better off making a number of approximations based on the fact that the path you are traversing is much less than the circumference of the earth. Then you could arrive at a much simpler formula.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文