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
