计算两个 GPS 点之间的 (x, y) 距离 [英] Calculate distance in (x, y) between two GPS-Points

查看:33
本文介绍了计算两个 GPS 点之间的 (x, y) 距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种平滑的方法来计算两个 GPS 点之间的距离,所以我得到的结果如下:你必须向上 x 米,向左 y 米 - 所以我可以使用 2d-坐标系,我的位置为 (0,0),其他位置显示 (x, y) 距离我的位置的距离,以米为单位.

I'm looking for a smooth way to calculate the distance between two GPS Points, so I get the result like: "You have to go x meters up and y meters to the left - so I can work with a 2d-coordinate system, where I have my position as (0,0) and the other positions is showing the distance in (x, y) in meters from my position.

我的想法是使用半正弦公式计算点之间的距离.(这会返回我的斜边)

My idea was to calculate the distance between the points using the haversine formula. (This returns my hypotenuse)

除此之外,我还在计算这两点之间的方位角.这是我的 Alpha.

In addition to that, I'm calculating the bearing between this two points. This is my alpha.

有了这两个值,我想使用基本的三角函数来解决我的问题.

With this two values, I wanted to use basic trigonometry functions to resolve my problem.

所以我试着计算:catheti_1 = sin(alpha) * hypotenuse,catheti_2 = cos(alpha) * hypotenuse.

也许我做错了什么,但目前我的结果毫无用处.

Maybe I'm doing something wrong, but my results are useless at the moment.

所以我的问题是:如何计算两个 GPS 点之间的 x 和 y 方向的距离?

So my question is: How can I calculate the distance in x and y direction between two GPS points?

我正在按照以下过程计算 alpha:

I'm calculating alpha in the following procedure:

public static double bearingTo(GPSBean point1, GPSBean point2) {
    double lat1 = Math.toRadians(point1.latitude);
    double lat2 = Math.toRadians(point2.latitude);
    double lon1 = Math.toRadians(point1.longitude);
    double lon2 = Math.toRadians(point2.longitude);

    double deltaLong = lon2 - lon1;

    double y = Math.sin(deltaLong) * Math.cos(lat2);
    double x = Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1)
            * Math.cos(lat2) * Math.cos(deltaLong);
    double bearing = Math.atan2(y, x);

    return (Math.toDegrees(bearing) + 360) % 360;
}

推荐答案

我刚刚实现了你的代码,使用纽约和波士顿的近似坐标作为参考点,并实现了在 http://www.movable-type.co.uk/scripts/latlong.html (你没有'不显示):

I just implemented your code, using approximate coordinates of NYC and Boston as reference points, and implementing the Haversine formula as found at http://www.movable-type.co.uk/scripts/latlong.html (which you didn't show):

long1 = -71.02; lat1 = 42.33;
long2 = -73.94; lat2 = 40.66;

lat1 *=pi/180;
lat2 *=pi/180;
long1*=pi/180;
long2*=pi/180;

dlong = (long2 - long1);
dlat  = (lat2 - lat1);

// Haversine formula:
R = 6371;
a = sin(dlat/2)*sin(dlat/2) + cos(lat1)*cos(lat2)*sin(dlong/2)*sin(dlong/2)
c = 2 * atan2( sqrt(a), sqrt(1-a) );
d = R * c;

当我运行此代码时,我得到 d = 306,这与上述网站的答案一致.

When I run this code, I get d = 306, which agrees with the answer from the above site.

对于方位,我得到 52 度 - 再次接近网站给出的值.

For the bearing I get 52 deg - again, close to what the site gave.

如果不查看您的其余代码,很难知道您的答案为何不同.

Without seeing the rest of your code it's hard to know why your answer is different.

注意:当两点靠得很近时,你可以做各种近似,但这段代码应该仍然有效——公式具有良好的数值稳定性,因为它使用了sin之间的差异经度,纬度(而不是罪的差).

Note: when the two points are close together, you could make all kinds of approximations, but this code should still work - the formula has good numerical stability because it's using the sin of the difference between longitudes, latitudes (rather than the difference of the sin).

附录:

使用您的 x、y 代码(在您的问题中),我得到了距离的合理值 - 同意 120 m 以内的正确"答案(这还不错,因为一个是直线近似值,而其他遵循地球的曲率).所以我认为你的代码基本上没问题,现在你修正了错字.

Using your code for x, y (in your question), I get sensible values for the distance - agreeing with the "proper" answer to within 120 m (which isn't bad since one is a straight line approximation and the other follows the curvature of the earth). So I think your code is basically OK now you fixed the typo.

这篇关于计算两个 GPS 点之间的 (x, y) 距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆