计算两个GPS点之间的距离(x,y) [英] Calculate distance in (x, y) between two GPS-Points
问题描述
我的想法是使用半束缚公式计算点之间的距离(这返回了我的斜边)除此之外,我正在计算这两点之间的方位。这是我的alpha。
有了这两个值,我想使用基本的三角函数来解决我的问题。
所以我试图计算:
catheti_1 = sin(alpha)*斜边,catheti_2 = cos(alpha)*斜边
。也许我做错了什么,但目前我的结果是无用的。
所以我的问题是:如何计算x和y方向之间een两个GPS点?
我正在计算以下过程中的alpha值:
public静态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;
}
使用纽约市和波士顿的大概坐标作为参考点,并实施Haversine公式,如 http://www.movable-type.co.uk/scripts/latlong.html (你没有显示):
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公式:
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
,与上述网站的答案一致。
对于轴承,我得到了52度 - 再次接近网站的内容。
如果没有看到其他代码,很难知道为什么你的答案不同。
注意:当两点靠近在一起,你可以做出各种近似值,但是这个代码仍然可以工作 - 这个公式具有很好的数值稳定性,因为它使用经度,纬度之间的差异( sin
而不是犯罪的差别)。
附录:使用您的代码x,y(in你的问题),我得到了合理的距离值 - 与120米内的正确答案一致(这并不坏,因为一个是直线近似,另一个跟随地球曲率)。所以我认为你的代码基本上可以,现在你修正了错字。
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)
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.
So I tried to calculate:catheti_1 = sin(alpha) * hypotenuse, catheti_2 = cos(alpha) * hypotenuse
.
Maybe I'm doing something wrong, but my results are useless at the moment.
So my question is: How can I calculate the distance in x and y direction between two GPS points?
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;
}
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;
When I run this code, I get d = 306
, which agrees with the answer from the above site.
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.
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).
Addendum:
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屋!