兩個(gè)點(diǎn)的經(jīng)緯度
latitude緯度 | longitude經(jīng)度 | 地點(diǎn) |
---|---|---|
22.678611 | 113.805695 | 深圳同泰萬怡酒店 |
22.716473 | 113.826391 | 深圳寶安中天美景華美達(dá)酒店 |
各種計(jì)算方式
計(jì)算方式 | 距離 |
---|---|
Elasticsearch:7.12.1 | 4715.088099751495 |
自定義公式計(jì)算 | 4720.367727793572 |
org.gavaghan/geodesy | 4715.085736444097 |
org.geotools/gt-referencing | 4701.260219872655 |
方式一:自定義公式計(jì)算
package com.example.demo.util;
public class GeoUtil {
/**
* 地球半徑,單位m
*/
private static final double EARTH_RADIUS = 6378137;
/**
* 根據(jù)經(jīng)緯度,計(jì)算兩點(diǎn)間的距離
*
* @param longitude1 第一個(gè)點(diǎn)的經(jīng)度
* @param latitude1 第一個(gè)點(diǎn)的緯度
* @param longitude2 第二個(gè)點(diǎn)的經(jīng)度
* @param latitude2 第二個(gè)點(diǎn)的緯度
* @return 返回距離,單位m
*/
public static double getDistance(double longitude1, double latitude1, double longitude2, double latitude2) {
// 緯度
double lat1 = Math.toRadians(latitude1);
double lat2 = Math.toRadians(latitude2);
// 經(jīng)度
double lng1 = Math.toRadians(longitude1);
double lng2 = Math.toRadians(longitude2);
// 緯度之差
double a = lat1 - lat2;
// 經(jīng)度之差
double b = lng1 - lng2;
// 計(jì)算兩點(diǎn)距離的公式
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計(jì)算距離
依賴
<!--用于計(jì)算兩點(diǎn)之間的距離-->
<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() {
//創(chuàng)建GeodeticCalculator,調(diào)用計(jì)算方法,傳入坐標(biāo)系、經(jīng)緯度用于計(jì)算距離
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
}
}
參考
計(jì)算兩個(gè)坐標(biāo)經(jīng)緯度之間的距離(5種方式)
方式三:geotools計(jì)算距離
文檔
- https://geotools.org/
依賴
<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
,不能設(shè)置為*
<mirrors>
<mirror>
<id>aliyunmaven</id>
<mirrorOf>central</mirrorOf>
<name>阿里云公共倉庫</name>
<url>https://maven.aliyun.com/repository/public</url>
</mirror>
</mirrors>
代碼示例文章來源:http://www.zghlxwxcb.cn/news/detail-785327.html
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坐標(biāo)系構(gòu)造GeodeticCalculator
GeodeticCalculator geodeticCalculator = new GeodeticCalculator(DefaultGeographicCRS.WGS84);
// 起點(diǎn)經(jīng)緯度
geodeticCalculator.setStartingGeographicPoint(longitude1, latitude1);
// 末點(diǎn)經(jīng)緯度
geodeticCalculator.setDestinationGeographicPoint(longitude2, latitude2);
// 計(jì)算距離,單位:米
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計(jì)算兩個(gè)GPS坐標(biāo)點(diǎn)之間的距離(可用于計(jì)算里程等)文章來源地址http://www.zghlxwxcb.cn/news/detail-785327.html
到了這里,關(guān)于Java:計(jì)算地球上兩個(gè)經(jīng)緯度坐標(biāo)之間的距離-geodesy和geotools實(shí)現(xiàn)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!