当前位置:网站首页>Calculate the distance between the longitude and latitude of two coordinates (5 ways)
Calculate the distance between the longitude and latitude of two coordinates (5 ways)
2022-07-24 12:06:00 【Xiao Bai loves learning】
Catalog
1.POM Introducing third party dependencies :
summary
Calculate the distance between two coordinates , Don't talk much , To make up !!!! Take it , You're welcome ....
Mode one
Arccosine calculation method
/**
* Earth radius , Company m
*/
private static final double EARTH_RADIUS = 6378137;
/**
* According to latitude and longitude , Calculate the distance between two points
*
* @param longitude1 Longitude of the first point
* @param latitude1 The latitude of the first point
* @param longitude2 The longitude of the second point
* @param latitude2 The latitude of the second point
* @return Return distance , Company m
*/
public static double getDistance1(double longitude1, double latitude1, double longitude2, double latitude2) {
// latitude
double lat1 = Math.toRadians(latitude1);
double lat2 = Math.toRadians(latitude2);
// longitude
double lng1 = Math.toRadians(longitude1);
double lng2 = Math.toRadians(longitude2);
// The difference in latitude
double a = lat1 - lat2;
// Difference of longitude
double b = lng1 - lng2;
// A formula for calculating the distance between two points
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)));
// The arc length times the radius of the earth , Return unit : rice
s = s * EARTH_RADIUS;
return s;
}
Mode two
be based on googleMap The algorithm in gets the distance between two longitudes and latitudes , Calculation precision The distance accuracy is similar to that of Google Maps .
/**
* Default earth radius , Equatorial radius ( Company m)
*/
private static double EARTH_RADIUS1 = 6371000;
/**
* Into radians (rad)
*/
private static double rad(double d)
{
return d * Math.PI / 180.0;
}
/**
* @param lon1 The accuracy of the first point
* @param lat1 The latitude of the first point
* @param lon2 Accuracy of the second point
* @param lat2 The latitude of the second point
* @return The distance back , Company m
* */
public static double getDistance2(double lon1,double lat1,double lon2, double lat2) {
double radLat1 = rad(lat1);
double radLat2 = rad(lat2);
double a = radLat1 - radLat2;
double b = rad(lon1) - rad(lon2);
double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
s = s * EARTH_RADIUS1;
s = Math.round(s * 10000) / 10000;
return s;
}Mode three
Arccosine calculation method
private static final double EARTH_RADIUS11 = 6371000; // Average radius , Company :m; Not equatorial radius . Equator is 6378 about
public static double getDistance3(Double lat1,Double lng1,Double lat2,Double lng2) {
// Longitude and latitude ( angle ) Arc of revolution . Radians are used as parameters , By calling Math.cos and Math.sin
double radiansAX = Math.toRadians(lng1); // A Through radian
double radiansAY = Math.toRadians(lat1); // A Latitude radian
double radiansBX = Math.toRadians(lng2); // B Through radian
double radiansBY = Math.toRadians(lat2); // B Latitude radian
// In the formula “cosβ1cosβ2cos(α1-α2)+sinβ1sinβ2” Part of , obtain ∠AOB Of cos value
double cos = Math.cos(radiansAY) * Math.cos(radiansBY) * Math.cos(radiansAX - radiansBX)
+ Math.sin(radiansAY) * Math.sin(radiansBY);
// System.out.println("cos = " + cos); // range [-1,1]
double acos = Math.acos(cos); // Arccosine value
// System.out.println("acos = " + acos); // range [0,π]
// System.out.println("∠AOB = " + Math.toDegrees(acos)); // Ball center angle range [0,180]
return EARTH_RADIUS11 * acos; // final result
}Mode 4
Leverage third parties jar Package calculation
1.POM Introducing third party dependencies :
<!-- Used to calculate the distance between two points -->
<dependency>
<groupId>org.gavaghan</groupId>
<artifactId>geodesy</artifactId>
<version>1.1.3</version>
</dependency>2. Code :
/**
* Calculate the distance between two longitudes and latitudes
* @param gpsFrom The first longitude and latitude
* @param gpsTo The second longitude and latitude
* @param ellipsoid Calculation method
* @return The distance back , Company m
*/
public static double getDistanceMeter(GlobalCoordinates gpsFrom, GlobalCoordinates gpsTo, Ellipsoid ellipsoid)
{
// establish GeodeticCalculator, Call the calculation method , Pass in the coordinate system 、 Longitude and latitude are used to calculate distance
GeodeticCurve geoCurve = new GeodeticCalculator().calculateGeodeticCurve(ellipsoid, gpsFrom, gpsTo);
return geoCurve.getEllipsoidalDistance();
}3. Perform the operation :
public static void main(String[] args) {
double lon1 = 39.111111;
double lat1 = 116.111111;
double lon2 = 39.222222;
double lat2 = 116.222222;
GlobalCoordinates source = new GlobalCoordinates(lon1, lat1);
GlobalCoordinates target = new GlobalCoordinates(lon2, lat2);
double meter1 = getDistanceMeter(source, target, Ellipsoid.Sphere);
double meter2 = getDistanceMeter(source, target, Ellipsoid.WGS84);
System.out.println("Sphere Coordinate system calculation results :"+meter1 + " rice ");
System.out.println("WGS84 Coordinate system calculation results :"+meter2 + " rice ");
}4. Execution results :
Sphere Coordinate system calculation results :15633.361234640292 rice
WGS84 Coordinate system calculation results :15632.349374386973 rice
Suggest
I passed these tests , Feedback based on the results , Feeling mode 4 Sphere The deviation of the coordinate system calculation result is relatively small .
Look forward to your triple strike , Your support is my motivation . come on. , Ape friends !!
边栏推荐
- makefile快速使用
- [TA frost wolf umay - "hundred people plan] Figure 3.3 surface subdivision and geometric shader large-scale grass rendering
- Browser logic vulnerability collection
- Chapter 0 Introduction and environment configuration
- Basic syntax of MySQL DDL and DML and DQL
- Three small knowledge points about data product managers
- L1-064 估值一亿的AI核心代码
- The art of management - driving software R & D efficiency through leadership
- Easy to use example
- Hash - 202. Happy number
猜你喜欢

Microsoft SQL Server database language and function usage (XII)

MySQL advanced (XVII) cannot connect to database server problem analysis
![Operational amplifier - Notes on rapid recovery [1] (parameters)](/img/1f/37c5548ce84b6a217b4742431f1cc4.png)
Operational amplifier - Notes on rapid recovery [1] (parameters)

Miss waiting for a year! Baidu super chain digital publishing service is limited to 50% discount

In kuborad graphical interface, operate kubernetes cluster to realize master-slave replication in MySQL

Record a garbage collection and analysis of gceasy
![[data mining engineer - written examination] sheen company in 2022](/img/67/c61dfce8c397ab55fab835b6e81a5f.jpg)
[data mining engineer - written examination] sheen company in 2022
![[mathematical basis of Cyberspace Security Chapter 3] congruence](/img/00/42a5f7f6f0e8a50884f4639767949f.jpg)
[mathematical basis of Cyberspace Security Chapter 3] congruence

Install JMeter

Use of multithreading in QT
随机推荐
Agile? DevOps ?
离散分布常用公式及应用场景
makefile快速使用
Shell Scripting tips
Dry goods sharing - taking over a new data team as a lead - Problem Inventory and insights findings
What is prescaler in STM32
Day3: branch structure
Chapter 1 Introduction
L1-043 阅览室
Svn server and client installation (Chinese package) and simple use
L1-064 估值一亿的AI核心代码
Literature record (part109) -- self representation based unsupervised exemplar selection in a union of subspaces
One week's wonderful content sharing (issue 13)
How to use a third party without obtaining root permission topic: MIUI chapter
Delphi gexperts expert instructions for improving development efficiency
第1章 引言
AcWing 92. 递归实现指数型枚举
[Commons beanautils topic] 005- convertutils topic
Jackson parsing JSON detailed tutorial
Optimization method of "great mathematics for use" -- optimal design of Cascade Reservoir Irrigation