简体   繁体   English

计算Java中两个地理坐标之间的距离

[英]Calculate the distance between two Geographical Coordinates in Java

I have two GPS Coordinates, for example: (44.40239182909422, 8.930511474608954) and (30.297017883371236, 122.3822021484364) 我有两个GPS坐标,例如: (44.40239182909422, 8.930511474608954)(30.297017883371236, 122.3822021484364)

I would like to know the distance in meters between these two points. 我想知道这两点之间的距离(以米为单位)。 I don't know if the first coordinate is greater than the second or not. 我不知道第一个坐标是否大于第二个坐标。

I am trying to understand and modify this code example: 我正在尝试理解和修改此代码示例:

 private double _distance(double lat1, double lon1, double lat2, double lon2) {
      double theta = lon1 - lon2;
      double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));
      dist = Math.acos(dist);
      dist = rad2deg(dist);
      dist = dist * 60 * 1.1515; // 60 is the number of minutes in a degree;  //1.1515 is the number of statute miles in a nautical mile.One nautical mile is the length of one minute of latitude at the equator.
      dist = dist * 1.609344;

      return (dist);
}

To calculate the 'theta' I added the following code: 为了计算“ theta”,我添加了以下代码:

double theta = lon1 - lon2;

if(lon2>lon1)
    theta = lon2 - lon1;

The distance function will return the distance between two points in meteres 距离函数将返回以米为单位的两点之间的距离

public double distance() {

    double lat1 = 44.40239182909422;
    double lon1 = 8.930511474608954;
    double lat2 = 30.297017883371236;
    double lon2 = 122.3822021484364;
    double theta = lon1 - lon2;
    double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));
    dist = Math.acos(dist);
    dist = rad2deg(dist);
    dist = dist * 1.609344 * 1000;        
    return (dist); // 134910.69784909734
}
    /* The function to convert decimal into radians */
private double deg2rad(double deg) {
    return (deg * Math.PI / 180.0);
}       
    /* The function to convert radians into decimal */
private double rad2deg(double rad) {
    return (rad * 180.0 / Math.PI);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM