Java:计算地球上两个经纬度坐标之间的距离-geodesy和geotools实现
•
Jave
目录
-
- 方式一:自定义公式计算
- 方式二:geodesy计算距离
- 方式三:geotools计算距离
两个点的经纬度
| latitude纬度 | longitude经度 | 地点 |
|---|---|---|
| 22.678611 | 113.805695 | 深圳同泰万怡酒店 |
| 22.716473 | 113.826391 | 深圳宝安中天美景华美达酒店 |
各种计算方式
| 计算方式 | 距离 |
|---|---|
| Elasticsearch:7.12.1 | 4715.088099751495 |
| 自定义公式计算 | 4720.367727793572 |
| org.gavaghan/geodesy | 4715.085736444097 |
| org.geotools/gt-referencing | 4701.260219872655 |
方式一:自定义公式计算
package com.example.demo.util;
public class GeoUtil {
/**
* 地球半径,单位m
*/
private static final double EARTH_RADIUS = 6378137;
/**
* 根据经纬度,计算两点间的距离
*
* @param longitude1 第一个点的经度
* @param latitude1 第一个点的纬度
* @param longitude2 第二个点的经度
* @param latitude2 第二个点的纬度
* @return 返回距离,单位m
*/
public static double getDistance(double longitude1, double latitude1, double longitude2, double latitude2) {
// 纬度
double lat1 = Math.toRadians(latitude1);
double lat2 = Math.toRadians(latitude2);
// 经度
double lng1 = Math.toRadians(longitude1);
double lng2 = Math.toRadians(longitude2);
// 纬度之差
double a = lat1 - lat2;
// 经度之差
double b = lng1 - lng2;
// 计算两点距离的公式
double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(lat1) * Math.cos(lat2) * Math.pow(Math.sin(b / 2), 2)));
// 弧长乘地球半径, 返回单位: 米
return s * EARTH_RADIUS;
}
public static void main(String[] args) {
double distance = GeoUtil.getDistance(113.805695, 22.678611, 113.826391, 22.716473);
System.out.println(distance);
// 4720.367727793572
}
}
方式二:geodesy计算距离
依赖
org.gavaghan
geodesy
1.1.3
示例
package com.example.demo;
import org.gavaghan.geodesy.Ellipsoid;
import org.gavaghan.geodesy.GeodeticCalculator;
import org.gavaghan.geodesy.GeodeticCurve;
import org.gavaghan.geodesy.GlobalCoordinates;
import org.junit.jupiter.api.Test;
public class GeoTest {
@Test
public void testGetDistance3() {
//创建GeodeticCalculator,调用计算方法,传入坐标系、经纬度用于计算距离
GeodeticCurve geoCurve = new GeodeticCalculator().calculateGeodeticCurve(
Ellipsoid.Sphere,
new GlobalCoordinates(22.678611, 113.805695),
new GlobalCoordinates(22.716473, 113.826391)
);
System.out.println(geoCurve.getEllipsoidalDistance());
// WGS84 4701.260219874908
// Sphere 4715.085736444097
}
}
参考
计算两个坐标经纬度之间的距离(5种方式)
方式三:geotools计算距离
文档
- https://geotools.org/
依赖
osgeo
OSGeo Release Repository
https://repo.osgeo.org/repository/release/
false
true
osgeo-snapshot
OSGeo Snapshot Repository
https://repo.osgeo.org/repository/snapshot/
true
false
org.geotools
gt-referencing
27.2
注意:geotools不在central中央仓库,需要配置下载源
如果配置了镜像 ~/.m2/settings.xml,不能设置为*
aliyunmaven
central
阿里云公共仓库
https://maven.aliyun.com/repository/public
代码示例
package com.example.demo.util;
import org.geotools.referencing.GeodeticCalculator;
import org.geotools.referencing.crs.DefaultGeographicCRS;
public class DemoUtil {
public static double getDistance(double longitude1, double latitude1, double longitude2, double latitude2) {
// 84坐标系构造GeodeticCalculator
GeodeticCalculator geodeticCalculator = new GeodeticCalculator(DefaultGeographicCRS.WGS84);
// 起点经纬度
geodeticCalculator.setStartingGeographicPoint(longitude1, latitude1);
// 末点经纬度
geodeticCalculator.setDestinationGeographicPoint(longitude2, latitude2);
// 计算距离,单位:米
return geodeticCalculator.getOrthodromicDistance();
}
public static void main(String[] args) {
double distance = DemoUtil.getDistance(113.805695, 22.678611, 113.826391, 22.716473);
System.out.println(distance);
// 4701.260219872655
}
}
参考
GeoTools依赖使用Maven下载失败解决办法记录
Java计算两个GPS坐标点之间的距离(可用于计算里程等)
本文来自网络,不代表协通编程立场,如若转载,请注明出处:https://net2asp.com/274677463d.html
