Java Distance Calculate distance(double lat1, double lng1, double lat2, double lng2)

Here you can find the source of distance(double lat1, double lng1, double lat2, double lng2)

Description

distance

License

Open Source License

Declaration

private static double distance(double lat1, double lng1, double lat2, double lng2) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/* w w w  .  ja  v a 2s  . com*/
     * 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.
     * <p>
     * lat1, lng1 Start point lat2, lng2 End point el1 Start altitude in meters
     * el2 End altitude in meters
     *
     * @return Distance in Meters
     */
    private static double distance(double lat1, double lng1, double el1, double lat2, double lng2, double el2) {

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

        double latDistance = Math.toRadians(lat2 - lat1);
        double lonDistance = Math.toRadians(lng2 - lng1);
        double a = Math.sin(latDistance / 2) * Math.sin(latDistance / 2) + Math.cos(Math.toRadians(lat1))
                * Math.cos(Math.toRadians(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

        double height = el1 - el2;

        distance = Math.pow(distance, 2) + Math.pow(height, 2);

        return Math.sqrt(distance);
    }

    private static double distance(double lat1, double lng1, double lat2, double lng2) {
        return distance(lat1, lng1, 0, lat2, lng2, 0);
    }
}

Related

  1. dist(int x1, int y1, int x2, int y2)
  2. dist14(double d12, double d23, double d34, double theta123, double theta234, double phi)
  3. dist2Degrees(double dist, double radius)
  4. dist2Radians(double dist, double radius)
  5. distAlongRaySquared(double px, double py, double qx, double qy, double rx, double ry)
  6. distance(double lat1, double lng1, double lat2, double lng2)
  7. distance(double lat1, double lon1, double lat2, double lon2)
  8. distance(double lat1, double lon1, double lat2, double lon2)
  9. distance(double lat1, double lon1, double lat2, double lon2)