当您知道java中的经度和纬度时,以米为单位计算距离
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/837872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calculate distance in meters when you know longitude and latitude in java
提问by Espen Herseth Halvorsen
Possible Duplicate:
Working with latitude/longitude values in Java
可能的重复:
在 Java 中使用纬度/经度值
Duplicate:
复制:
- Working with latitude/longitude values in Java
- How do I calculate distance between two latitude longitude points?
I need to calculate the distance between two points given by two coordinates. The project I am working on is a Java-project, so Java-code will be great, but pseudo-code can also be given, then I can implement it myself :)
我需要计算由两个坐标给出的两点之间的距离。我在做的项目是Java项目,所以Java代码会很好,但是也可以给出伪代码,那我自己实现吧:)
As you probably know, there are three ways to represent coordinates:
您可能知道,有三种表示坐标的方法:
- Degrees:Minutes:Seconds (49°30'00"N, 123°30'00"W)
- Degrees:Decimal Minutes (49°30.0', -123°30.0'), (49d30.0m,-123d30.0')
- Decimal Degrees (49.5000°,-123.5000°), generally with 4-6 decimal numbers.
- 度:分:秒(49°30'00"N, 123°30'00"W)
- 度数:十进制分 (49°30.0', -123°30.0'), (49d30.0m,-123d30.0')
- 十进制度数(49.5000°,-123.5000°),一般有4-6个十进制数。
It's the third way my coordinates are given in, so the code for this values will be preferred :)
这是我的坐标的第三种方式,因此该值的代码将是首选:)
采纳答案by Espen Herseth Halvorsen
Based on another question on stackoverflow, I got this code.. This calculates the result in meters, not in miles :)
基于关于stackoverflow 的另一个问题,我得到了这个代码。这以米为单位计算结果,而不是以英里为单位:)
public static float distFrom(float lat1, float lng1, float lat2, float lng2) {
double earthRadius = 6371000; //meters
double dLat = Math.toRadians(lat2-lat1);
double dLng = Math.toRadians(lng2-lng1);
double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
Math.sin(dLng/2) * Math.sin(dLng/2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
float dist = (float) (earthRadius * c);
return dist;
}
回答by jessn
In C++ it is done like this:
在 C++ 中,它是这样完成的:
#define LOCAL_PI 3.1415926535897932385
double ToRadians(double degrees)
{
double radians = degrees * LOCAL_PI / 180;
return radians;
}
double DirectDistance(double lat1, double lng1, double lat2, double lng2)
{
double earthRadius = 3958.75;
double dLat = ToRadians(lat2-lat1);
double dLng = ToRadians(lng2-lng1);
double a = sin(dLat/2) * sin(dLat/2) +
cos(ToRadians(lat1)) * cos(ToRadians(lat2)) *
sin(dLng/2) * sin(dLng/2);
double c = 2 * atan2(sqrt(a), sqrt(1-a));
double dist = earthRadius * c;
double meterConversion = 1609.00;
return dist * meterConversion;
}
回答by Oscar Salguero
You can use the Java Geodesy Library for GPS, it uses the Vincenty's formulaewhich takes account of the earths surface curvature.
您可以使用用于 GPS的Java Geodesy 库,它使用Vincenty 公式,该公式考虑了地球表面曲率。
Implementation goes like this:
实现是这样的:
import org.gavaghan.geodesy.*;
...
GeodeticCalculator geoCalc = new GeodeticCalculator();
Ellipsoid reference = Ellipsoid.WGS84;
GlobalPosition pointA = new GlobalPosition(latitude, longitude, 0.0); // Point A
GlobalPosition userPos = new GlobalPosition(userLat, userLon, 0.0); // Point B
double distance = geoCalc.calculateGeodeticCurve(reference, userPos, pointA).getEllipsoidalDistance(); // Distance between Point A and Point B
The resulting distance is in meters.
结果距离以米为单位。