两个点的经纬度
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
- }
- }
-
-
依赖
- <!--用于计算两点之间的距离-->
- <dependency>
- <groupId>org.gavaghan</groupId>
- <artifactId>geodesy</artifactId>
- <version>1.1.3</version>
- </dependency>
-
示例
- 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
- }
- }
-
-
参考
文档
依赖
- <repositories>
- <repository>
- <id>osgeo</id>
- <name>OSGeo Release Repository</name>
- <url>https://repo.osgeo.org/repository/release/</url>
- <snapshots><enabled>false</enabled></snapshots>
- <releases><enabled>true</enabled></releases>
- </repository>
- <repository>
- <id>osgeo-snapshot</id>
- <name>OSGeo Snapshot Repository</name>
- <url>https://repo.osgeo.org/repository/snapshot/</url>
- <snapshots><enabled>true</enabled></snapshots>
- <releases><enabled>false</enabled></releases>
- </repository>
- </repositories>
-
- <dependencies>
- <dependency>
- <groupId>org.geotools</groupId>
- <artifactId>gt-referencing</artifactId>
- <version>27.2</version>
- </dependency>
- </dependencies>
-
-
注意:geotools不在central中央仓库,需要配置下载源
如果配置了镜像 ~/.m2/settings.xml,不能设置为*
- <mirrors>
- <mirror>
- <id>aliyunmaven</id>
- <mirrorOf>central</mirrorOf>
- <name>阿里云公共仓库</name>
- <url>https://maven.aliyun.com/repository/public</url>
- </mirror>
- </mirrors>
-
代码示例
- 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
- }
- }