1. 程式人生 > 其它 >JAVA中計算經緯度之間的距離

JAVA中計算經緯度之間的距離

1.匯入需要的JAR包

<!-- https://mvnrepository.com/artifact/org.gavaghan/geodesy -->
<dependency>
    <groupId>org.gavaghan</groupId>
    <artifactId>geodesy</artifactId>
    <version>1.1.3</version>
</dependency>

  

2.程式碼如下

@Test
public void testGPS(){
    //給定兩個座標系,計算兩點相差距離
    GlobalCoordinates source = new GlobalCoordinates(29.490295, 106.486654);
    GlobalCoordinates target = new GlobalCoordinates(29.615467, 106.581515);
    //Sphere座標的計算結果
    double meter1 =getDistanceMeter(source,target,Ellipsoid.Sphere);
    //WGS84座標系計算結果
    double meter2 = getDistanceMeter(source,target,Ellipsoid.WGS84);

    //計算結果Sphere 座標系的計算結果與 WGS84座標系的計算結果存在幾十米的誤差,不同的座標系精度不同,
    System.out.println("Sphere 座標系的計算結果是" + meter1 + "M");
    System.out.println("wGS84  座標系的計算結果是" + meter2 + "M");
}



public static  double getDistanceMeter(GlobalCoordinates gpsFrom, GlobalCoordinates gpsTo, Ellipsoid ellipsoid){
    //建立GeodeticCalculator,呼叫計算方法,傳入座標系,經緯度用於計算距離
    GeodeticCurve geoCurve = new GeodeticCalculator().calculateGeodeticCurve(ellipsoid, gpsFrom, gpsTo);
    return geoCurve.getEllipsoidalDistance();
}

如有錯誤,請指正! 聯絡wx 15514769010