【问题标题】:How can i calculate the distance between two gps points in Java?我如何计算Java中两个gps点之间的距离?
【发布时间】:2011-04-12 13:06:05
【问题描述】:

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

需要两个 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();
}

【问题讨论】:

  • 如果地球是一个完美的球体,您的三角法方法将非常有效。然而,它实际上是一个椭球体,所以从地表到地核的半径是可变的,这取决于你在地球上的位置。要获得准确的距离测量,您需要获得一个可以解决此问题的坐标系库。假设一个球体对于你所做的任何事情来说可能足够接近,但如果你需要它非常准确,你需要找到一个好的库。

标签: java gps coordinates distance geo


【解决方案1】:

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

即改变

double d2r = (180 / Math.PI);

进入

double d2r = Math.PI / 180;

【讨论】:

    【解决方案2】:

    看看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 公里

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

    【讨论】:

      【解决方案3】:

      来自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; } //

      Distance between two GPS coordinates in meter

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          【解决方案5】:

          【讨论】:

            【解决方案6】:

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

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2014-02-28
              • 2014-11-14
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多