22

我使用了这段代码,但它不起作用:

需要两个 gps 坐标之间的距离,例如 41.1212、11.2323 公里(Java)

double d2r = (180 / Math.PI);
double distance = 0;

try{
    double dlong = (endpoint.getLon() - startpoint.getLon()) * d2r;
    double dlat = (endpoint.getLat() - startpoint.getLat()) * d2r;
    double a =
        Math.pow(Math.sin(dlat / 2.0), 2)
            + Math.cos(startpoint.getLat() * d2r)
            * Math.cos(endpoint.getLat() * d2r)
            * Math.pow(Math.sin(dlong / 2.0), 2);
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    double d = 6367 * c;

    return d;

} catch(Exception e){
    e.printStackTrace();
}
4

6 回答 6

20

经度和纬度以度 (0-360) 为单位。如果要将度数转换为弧度 (0-2π),则需要除以 360 并乘以 2π,(或等效地,乘以 π/180)。但是,在您的代码中,您乘以 180/π

也就是改变

double d2r = (180 / Math.PI);

进入

double d2r = Math.PI / 180;
于 2010-09-15T07:36:29.683 回答
10

看看Geocalc

Coordinate lat = new GPSCoordinate(41, .1212);
Coordinate lng = new GPSCoordinate(11, .2323);
Point point = new Point(lat, lng);

lat = new DegreeCoordinate(51.4613418);
lng = new DegreeCoordinate(-0.3035466);
Point point2 = new Point(lat, lng);
System.out.println("Distance is " + EarthCalc.getDistance(point2, point) / 1000 + " km");

距离为 1448.7325760822912 公里

我为我的一个项目编写了那个库。

于 2012-08-19T01:26:04.720 回答
8

来自http://www.androidsnippets.com/distance-between-two-gps-coordinates-in-meter的解决方案。

仅当这些点足够接近以至于您可以忽略地球不是规则形状时才有效。

私人双gps2m(浮动lat_a,浮动lng_a,浮动lat_b,浮动lng_b){
    浮动PK =(浮动)(180/3.14169);

    浮动 a1 = lat_a / pk;
    浮动 a2 = lng_a / pk;
    浮动 b1 = lat_b / pk;
    浮动 b2 = lng_b / pk;

    浮点数 t1 = FloatMath.cos(a1)*FloatMath.cos(a2)*FloatMath.cos(b1)*FloatMath.cos(b2);
    浮点数 t2 = FloatMath.cos(a1)*FloatMath.sin(a2)*FloatMath.cos(b1)*FloatMath.sin(b2);
    浮点数 t3 = FloatMath.sin(a1)*FloatMath.sin(b1);
    双 tt = Math.acos(t1 + t2 + t3);

    返回 6366000*tt;
}
//

请参阅以米为单位的两个 GPS 坐标之间的距离

于 2011-07-22T08:05:34.870 回答
2

两点之间的距离(以及许多其他有用的东西)可以在以下位置找到:http ://www.movable-type.co.uk/scripts/latlong.html

于 2011-12-30T07:46:30.960 回答
1

http://www.androidsnippets.com/distance-between-two-gps-coordinates-in-meter 显示 double d2r = (180 / Math.PI);

于 2011-04-26T18:27:36.053 回答
0

可以使用 Esri 几何库计算距离:geodesicDistanceOnWGS84

我会假设 JTS 也有一种计算距离的方法。

于 2016-12-16T22:05:58.553 回答