如何计算Java中两个gps点之间的距离?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3715521/
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
How can i calculate the distance between two gps points in Java?
提问by Benni
I used this code but it doesnt work:
我使用了这段代码,但它不起作用:
Need the distance between two gps coordinates like 41.1212, 11.2323 in kilometers (Java)
需要两个 gps 坐标之间的距离,如 41.1212、11.2323(以公里为单位)(Java)
double d2r = (180 / Math.PI);
double distance = 0;
try{
double dlong = (endpoint.getLon() - startpoint.getLon()) * d2r;
double dlat = (endpoint.getLat() - startpoint.getLat()) * d2r;
double a =
Math.pow(Math.sin(dlat / 2.0), 2)
+ Math.cos(startpoint.getLat() * d2r)
* Math.cos(endpoint.getLat() * d2r)
* Math.pow(Math.sin(dlong / 2.0), 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
double d = 6367 * c;
return d;
} catch(Exception e){
e.printStackTrace();
}
回答by aioobe
Longitude and latitude are given in degrees (0-360). If you want to convert degrees to radians (0-2π) you need to divide by 360 and multiply by 2π, (or equivalently, multiply by π/180). In your code however, you multiply by 180/π.
经度和纬度以度数 (0-360) 给出。如果要将度数转换为弧度 (0-2π),则需要除以 360 并乘以 2π,(或等效地乘以 π/180)。但是,在您的代码中,您乘以 180/π。
That is, change
也就是说,改变
double d2r = (180 / Math.PI);
into
进入
double d2r = Math.PI / 180;
回答by M.Hefny
http://www.androidsnippets.com/distance-between-two-gps-coordinates-in-metershows that double d2r = (180 / Math.PI);
http://www.androidsnippets.com/distance-between-two-gps-coordinates-in-meter显示 double d2r = (180 / Math.PI);
回答by sherif
The solution from http://www.androidsnippets.com/distance-between-two-gps-coordinates-in-meter.
来自http://www.androidsnippets.com/distance-between-two-gps-coordinates-in-meter的解决方案。
Only works if the points are close enough that you can omit that earth is not regular shape.
仅当点足够接近以至于您可以忽略地球不是规则形状时才有效。
private double gps2m(float lat_a, float lng_a, float lat_b, float lng_b) { float pk = (float) (180/3.14169); float a1 = lat_a / pk; float a2 = lng_a / pk; float b1 = lat_b / pk; float b2 = lng_b / pk; float t1 = FloatMath.cos(a1)*FloatMath.cos(a2)*FloatMath.cos(b1)*FloatMath.cos(b2); float t2 = FloatMath.cos(a1)*FloatMath.sin(a2)*FloatMath.cos(b1)*FloatMath.sin(b2); float t3 = FloatMath.sin(a1)*FloatMath.sin(b1); double tt = Math.acos(t1 + t2 + t3); return 6366000*tt; } //
回答by peSHIr
Distance between two points (and lots of other useful things) can be found at: http://www.movable-type.co.uk/scripts/latlong.html
两点之间的距离(以及许多其他有用的东西)可以在以下位置找到:http: //www.movable-type.co.uk/scripts/latlong.html
回答by Romain
have a look a Geocalc
看看Geocalc
Coordinate lat = new GPSCoordinate(41, .1212);
Coordinate lng = new GPSCoordinate(11, .2323);
Point point = new Point(lat, lng);
lat = new DegreeCoordinate(51.4613418);
lng = new DegreeCoordinate(-0.3035466);
Point point2 = new Point(lat, lng);
System.out.println("Distance is " + EarthCalc.getDistance(point2, point) / 1000 + " km");
Distance is 1448.7325760822912 km
距离是 1448.7325760822912 公里
I wrote that library for one my project.
我为我的一个项目编写了该库。
回答by Randall Whitman
The distance can be calculated with the Esri Geometry library: geodesicDistanceOnWGS84.
可以使用 Esri 几何库计算距离:geodesicDistanceOnWGS84。
I would assume that JTS also has a method for computing distance.
我认为 JTS 也有一种计算距离的方法。