Java Distance Calculate distance(double lat1, double lon1, double lat2, double lon2)

Here you can find the source of distance(double lat1, double lon1, double lat2, double lon2)

Description

Calculate distance between two points in latitude and longitude taking into account height difference.

License

MIT License

Declaration

public static double distance(double lat1, double lon1, double lat2, double lon2) 

Method Source Code

//package com.java2s;
/*/*from  www.ja v a 2 s.  c o  m*/
 * GeoUtils.java - Copyright(c) 2013 Joe Pasqua
 * Provided under the MIT License. See the LICENSE file for details.
 * Created: Jul 10, 2013
 */

public class Main {
    /**
     * Calculate distance between two points in latitude and longitude taking
     * into account height difference. If you are not interested in height
     * difference pass 0.0. Uses Haversine method as its base.
     *
     * lat1, lon1 Start point lat2, lon2 End point 
     */
    public static double distance(double lat1, double lon1, double lat2, double lon2) {

        final int R = 6371; // Radius of the earth

        Double latDistance = deg2rad(lat2 - lat1);
        Double lonDistance = deg2rad(lon2 - lon1);
        Double a = Math.sin(latDistance / 2) * Math.sin(latDistance / 2) + Math.cos(deg2rad(lat1))
                * Math.cos(deg2rad(lat2)) * Math.sin(lonDistance / 2) * Math.sin(lonDistance / 2);
        Double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
        double distance = R * c * 1000; // convert to meters

        return distance;
    }

    private static double deg2rad(double deg) {
        return (deg * Math.PI / 180.0);
    }
}

Related

  1. dist2Radians(double dist, double radius)
  2. distAlongRaySquared(double px, double py, double qx, double qy, double rx, double ry)
  3. distance(double lat1, double lng1, double lat2, double lng2)
  4. distance(double lat1, double lng1, double lat2, double lng2)
  5. distance(double lat1, double lon1, double lat2, double lon2)
  6. distance(double lat1, double lon1, double lat2, double lon2)
  7. distance(double lat1, double lon1, double lat2, double lon2)
  8. distance(double latA, double logA, double latB, double logB)
  9. distance(double latDegrees1, double lonDegrees1, double latDegrees2, double lonDegrees2)