91名师指路-头部
91名师指路

java 计算2个经纬度之间的距离

由于某些原因,现在不支持支付宝支付,如需要购买源码请加博主微信进行购买,微信号:13248254750

一:引入pom

<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     阅读(546)

名师出品,必属精品    https://www.91mszl.com

联系博主    
用户登录遮罩层
x

账号登录

91名师指路-底部