经纬度计算距离
经纬度计算距离
1.导入maven依赖
org.gavaghan
是一个 Java 软件包,由 Tim Gavaghan 创建和维护。该软件包提供了一系列用于处理和解析各种数据格式的类和工具。
<dependency>
<groupId>org.gavaghan</groupId>
<artifactId>geodesy</artifactId>
<version>${geodesy.version}</version>
</dependency>
2.代码实现
GlobalCoordinates start = globalCoordinatesList.get(i);
GlobalCoordinates end = globalCoordinatesList.get(i + 1);
private static BigDecimal getDistanceMeter(GlobalCoordinates gpsFrom, GlobalCoordinates gpsTo) {
//创建GeodeticCalculator,调用计算方法,传入坐标系、经纬度用于计算距离
GeodeticCurve geoCurve = new GeodeticCalculator().calculateGeodeticCurve(Ellipsoid.Sphere, gpsFrom, gpsTo);
return BigDecimal.valueOf(geoCurve.getEllipsoidalDistance());
}