java计算二个经纬度间的距离(百度坐标)

1:背景

工作中遇到计算二个地点之间的距离,根据百度经纬度进行计算。

2:maven 依赖

<dependency>
  <groupId>org.gavaghan</groupId>
  <artifactId>geodesy</artifactId>
  <version>1.1.3</version>
</dependency>

3:代码实现

package com.pacific.transfer.util;

import org.gavaghan.geodesy.Ellipsoid;
import org.gavaghan.geodesy.GeodeticCalculator;
import org.gavaghan.geodesy.GeodeticCurve;
import org.gavaghan.geodesy.GlobalCoordinates;

/**
* 经纬度计算方法
*/
public class CaculateDistanceTest {
public static void main(String[] args) {

GlobalCoordinates source = new GlobalCoordinates(31.258934000000, 121.595511000000);
GlobalCoordinates target = new GlobalCoordinates(31.235124000000, 121.530746000000);

double meter1 = getDistanceMeter(source, target, Ellipsoid.Sphere);
double meter2 = getDistanceMeter(source, target, Ellipsoid.WGS84);
System.out.println("Sphere 坐标系计算结果:" + meter1 + "米");
System.out.println("WGS84 坐标系计算结果:" + meter2 + "米");
}

/**
* 计算距离
*
* @param from
* @param to
* @param ellipsoid
* @return
*/
public static double getDistanceMeter(GlobalCoordinates from, GlobalCoordinates to, Ellipsoid ellipsoid) {
// 经纬度计算距离
GeodeticCurve geoCurve = new GeodeticCalculator().calculateGeodeticCurve(ellipsoid, from, to);
return geoCurve.getEllipsoidalDistance();
}
}

4:结果如下:

Sphere 坐标系计算结果:6701.987620282907米
WGS84  坐标系计算结果:6710.41999653924米

 

参考地址:https://lbsyun.baidu.com/jsdemo.htm#a6_1

posted @ 2023-12-12 16:00  明天,你好啊  阅读(729)  评论(0编辑  收藏  举报