How can i calculate the distance between two gps points in Java?

浪尽此生 提交于 2019-11-30 01:40:41

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/π.

That is, change

double d2r = (180 / Math.PI);

into

double d2r = Math.PI / 180;
Romain

have a look a 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

I wrote that library for one my project.

sherif

The solution from 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;
}
// 

see Distance between two GPS coordinates in meter.

Distance between two points (and lots of other useful things) can be found at: http://www.movable-type.co.uk/scripts/latlong.html

The distance can be calculated with the Esri Geometry library: geodesicDistanceOnWGS84.

I would assume that JTS also has a method for computing distance.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!