计算两个gps点之间(x, y)的距离

时间:2021-12-17 04:05:59

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.

我寻找一个平滑的方法来计算两个GPS点之间的距离,得到这样的结果:“你必须去x米和米向左——所以我可以使用一个2维坐标系统,在那里我有我的地位(0,0),另一个位置显示距离(x,y)在米从我的位置。

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

我的想法是用haversine公式计算点之间的距离。(这将返回我的斜边)

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.

所以我试着计算:导管1 = sin() *斜边,导管2 = cos(-) *斜边。

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?

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

I'm calculating alpha in the following procedure:

我在下面的程序中计算alpha:

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

2 个解决方案

#1


5  

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

我刚刚实现了您的代码,使用NYC和Boston的近似坐标作为参考点,并实现了在http://www.movable-type.co.uk/scripts/latlong.html中找到的Haversine公式(您没有显示):

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.

当我运行这段代码时,我得到d = 306,这与上面站点的答案是一致的。

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

对于轴承,我得到52度-同样,接近网站给出的。

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

注意:当这两个点在一起的时候,你可以做各种近似,但是这个代码仍然可以工作——这个公式有很好的数值稳定性,因为它使用的是纵、纬度之间的差的sin(而不是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.

使用你的x, y(在你的问题中)的代码,我得到了距离的合理值——同意“正确”的答案在120米以内(这并不坏,因为一个是直线的近似,另一个是地球的曲率)。所以我认为你的代码基本上没问题现在你修正了错误。

#2


2  

Use Haversine formula to Calculate distance (in km) between two points specified by latitude/longitude (in numeric degrees)

使用Haversine公式计算纬度/经度指定的两点之间的距离(以公里为单位)

from: Haversine formula - R. W. Sinnott, "Virtues of the Haversine"

出处:Haversine formula - R. W. Sinnott,“Haversine美德”

Sky and Telescope, vol 68, no 2, 1984

《天空和望远镜》,第68卷,第2期,1984年。

http://www.census.gov/cgi-bin/geo/gisfaq?Q5.1

http://www.census.gov/cgi-bin/geo/gisfaq?Q5.1

Example usage from form:

示例使用来自表单:

result.value = LatLon.distHaversine(lat1.value.parseDeg(), long1.value.parseDeg(), * lat2.value.parseDeg(), long2.value.parseDeg());

Javascript :

Javascript:

LatLon.distHaversine = function(lat1, lon1, lat2, lon2) {
   var R = 6371; // earth's mean radius in km
   var dLat = (lat2-lat1).toRad();
   var dLon = (lon2-lon1).toRad();
   lat1 = lat1.toRad(), lat2 = lat2.toRad();
   var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
   Math.cos(lat1) * Math.cos(lat2) * Math.sin(dLon/2) * Math.sin(dLon/2);
   var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
   var d = R * c;

   return d;
}

#1


5  

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

我刚刚实现了您的代码,使用NYC和Boston的近似坐标作为参考点,并实现了在http://www.movable-type.co.uk/scripts/latlong.html中找到的Haversine公式(您没有显示):

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.

当我运行这段代码时,我得到d = 306,这与上面站点的答案是一致的。

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

对于轴承,我得到52度-同样,接近网站给出的。

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

注意:当这两个点在一起的时候,你可以做各种近似,但是这个代码仍然可以工作——这个公式有很好的数值稳定性,因为它使用的是纵、纬度之间的差的sin(而不是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.

使用你的x, y(在你的问题中)的代码,我得到了距离的合理值——同意“正确”的答案在120米以内(这并不坏,因为一个是直线的近似,另一个是地球的曲率)。所以我认为你的代码基本上没问题现在你修正了错误。

#2


2  

Use Haversine formula to Calculate distance (in km) between two points specified by latitude/longitude (in numeric degrees)

使用Haversine公式计算纬度/经度指定的两点之间的距离(以公里为单位)

from: Haversine formula - R. W. Sinnott, "Virtues of the Haversine"

出处:Haversine formula - R. W. Sinnott,“Haversine美德”

Sky and Telescope, vol 68, no 2, 1984

《天空和望远镜》,第68卷,第2期,1984年。

http://www.census.gov/cgi-bin/geo/gisfaq?Q5.1

http://www.census.gov/cgi-bin/geo/gisfaq?Q5.1

Example usage from form:

示例使用来自表单:

result.value = LatLon.distHaversine(lat1.value.parseDeg(), long1.value.parseDeg(), * lat2.value.parseDeg(), long2.value.parseDeg());

Javascript :

Javascript:

LatLon.distHaversine = function(lat1, lon1, lat2, lon2) {
   var R = 6371; // earth's mean radius in km
   var dLat = (lat2-lat1).toRad();
   var dLon = (lon2-lon1).toRad();
   lat1 = lat1.toRad(), lat2 = lat2.toRad();
   var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
   Math.cos(lat1) * Math.cos(lat2) * Math.sin(dLon/2) * Math.sin(dLon/2);
   var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
   var d = R * c;

   return d;
}