maven库导包
<dependency>
<groupId>org.gavaghan</groupId>
<artifactId>geodesy</artifactId>
<version>1.1.3</version>
</dependency>
import org.gavaghan.geodesy.Ellipsoid;
import org.gavaghan.geodesy.GeodeticCalculator;
import org.gavaghan.geodesy.GeodeticCurve;
import org.gavaghan.geodesy.GlobalCoordinates;
public class test {
public static void main(String[] args)
{
// //121.717594,31.12055 121.817629,31.090867
GlobalCoordinates source = new GlobalCoordinates(31.12055, 121.717594);
GlobalCoordinates target = new GlobalCoordinates(31.090867, 121.817629);
double meter1 = getDistanceMeter(source, target, Ellipsoid.Sphere);
double meter2 = getDistanceMeter(source, target, Ellipsoid.WGS84);
System.out.println("Sphere坐标系计算结果:"+meter1 + "米");
System.out.println("WGS84坐标系计算结果:"+meter2 + "米");
}
public static double getDistanceMeter(GlobalCoordinates gpsFrom, GlobalCoordinates gpsTo, Ellipsoid ellipsoid)
{
//创建GeodeticCalculator,调用计算方法,传入坐标系、经纬度用于计算距离
GeodeticCurve geoCurve = new GeodeticCalculator().calculateGeodeticCurve(ellipsoid, gpsFrom, gpsTo);
return geoCurve.getEllipsoidalDistance();
}
}
对比百度地图,计算结果和Sphere坐标系计算结果一致,表明计算结果正确,WGS84坐标系的计算结果存在几十米的误差。不同的坐标系精度不同,计算结果不一样。