原 java 计算2个经纬度之间的距离
版权声明:本文为博主原创文章,请尊重他人的劳动成果,转载请附上原文出处链接和本声明。
本文链接:https://www.91mszl.com/zhangwuji/article/details/1412
<dependency>
<groupId>org.gavaghan</groupId>
<artifactId>geodesy</artifactId>
<version>1.1.3</version>
</dependency>
package com.gaia.common.util;
import org.gavaghan.geodesy.Ellipsoid;
import org.gavaghan.geodesy.GeodeticCalculator;
import org.gavaghan.geodesy.GeodeticCurve;
import org.gavaghan.geodesy.GlobalCoordinates;
/**
* 功能:计算2个经纬度之间的距离
* 备注:https://91mszl.com
* @Author: zxb
* @Date: 2022-08-30 07:58:49
*/
public class CalculateLngLatDistanceUtils {
public static void main(String[] args){
double sourceLng=116.169026;
double sourceLat=23.305182;
double targetLng=116.169205;
double targetLat=23.29743;
GlobalCoordinates source=new GlobalCoordinates(sourceLat, sourceLng);
GlobalCoordinates target=new GlobalCoordinates(targetLat, targetLng);
double meter1 = getDistanceMeter(source, target, Ellipsoid.Sphere);
System.out.println("Sphere坐标系计算结果:"+meter1 + "米");
double meter2 = getDistanceMeter(source, target, Ellipsoid.WGS84);
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坐标系计算结果:862.1768908806453米
WGS84坐标系计算结果:858.715609866341米
2022-08-30 08:02:43 阅读(547)
名师出品,必属精品 https://www.91mszl.com
博主信息