需求:传入两个经纬度 计算两点之间的 距离
1.导入依赖的jar
<!--计算距离依赖的jar-->
<dependency>
<groupId>org.gavaghan</groupId>
<artifactId>geodesy</artifactId>
<version>1.1.3</version>
</dependency>
2.简单的工具类
import org.gavaghan.geodesy.Ellipsoid;
import org.gavaghan.geodesy.GeodeticCalculator;
import org.gavaghan.geodesy.GeodeticCurve;
import org.gavaghan.geodesy.GlobalCoordinates;
/**
* 计算经纬度直线距离
* @author: xiaoyc
*/
public class LonLatUtil {
public static double getDistanceMeter(GlobalCoordinates gpsFrom, GlobalCoordinates gpsTo, Ellipsoid ellipsoid) {
//创建GeodeticCalculator,调用计算方法,传入坐标系、经纬度用于计算距离
GeodeticCurve geoCurve = new GeodeticCalculator().calculateGeodeticCurve(ellipsoid, gpsFrom, gpsTo);
return geoCurve.getEllipsoidalDistance();
}
}
DEMO
import com.xiangwang.utils.LonLatUtil;
import org.gavaghan.geodesy.Ellipsoid;
import org.gavaghan.geodesy.GlobalCoordinates;
public class MeterTest {
public static void main(String[] args) {
//用户当前坐标
GlobalCoordinates userSource = new GlobalCoordinates(49.978957, 116.419715);
//店铺位置坐标
GlobalCoordinates businessTarget = new GlobalCoordinates(49.986348, 116.493068);
double meterDouble = LonLatUtil.getDistanceMeter(userSource, businessTarget, Ellipsoid.Sphere);
System.out.println(meterDouble);
}
}