Java Distance Calculate distanceBetween(double lon, double lat, double otherLon, double otherLat)

Here you can find the source of distanceBetween(double lon, double lat, double otherLon, double otherLat)

Description

Returns the distance between two locations.

License

Open Source License

Parameter

Parameter Description
lon longitude, in degrees, of the first data location
lat latitude, in degrees, of the first data location
otherlon longitude, in degrees, of the other data location
otherlat latitude, in degrees, of the other data location

Return

the location-time distance between this location-time point and other in kilometers

Declaration

public static double distanceBetween(double lon, double lat, double otherLon, double otherLat) 

Method Source Code

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

public class Main {
    /** Authalic radius, in kilometers, of Earth */
    public static final double EARTH_AUTHALIC_RADIUS = 6371.007;

    /**//from  ww w  .  j a  v a 2 s  . c  om
     * Returns the distance between two locations.  Uses the haversine formula, 
     * and {@link DashboardUtils#EARTH_AUTHALIC_RADIUS} for the radius of a 
     * spherical Earth, to compute the great circle distance from the 
     * longitudes and latitudes.
     * 
     * @param lon
     *       longitude, in degrees, of the first data location
     * @param lat
     *       latitude, in degrees, of the first data location
     * @param otherlon
     *       longitude, in degrees, of the other data location
     * @param otherlat
     *       latitude, in degrees, of the other data location
     * @return
     *      the location-time distance between this location-time point
     *      and other in kilometers
     */
    public static double distanceBetween(double lon, double lat, double otherLon, double otherLat) {
        // Convert longitude and latitude degrees to radians
        double lat1 = lat * Math.PI / 180.0;
        double lat2 = otherLat * Math.PI / 180.0;
        double lon1 = lon * Math.PI / 180.0;
        double lon2 = otherLon * Math.PI / 180.0;
        /*
         * Use the haversine formula to compute the great circle distance, 
         * in radians, between the two (longitude, latitude) points. 
         */
        double dellat = Math.sin(0.5 * (lat2 - lat1));
        dellat *= dellat;
        double dellon = Math.sin(0.5 * (lon2 - lon1));
        dellon *= dellon * Math.cos(lat1) * Math.cos(lat2);
        double distance = 2.0 * Math.asin(Math.sqrt(dellon + dellat));
        // Convert the great circle distance from radians to kilometers
        distance *= EARTH_AUTHALIC_RADIUS;

        return distance;
    }
}

Related

  1. distance_to_endpoint(int x1, int y1, int x2, int y2, int x, int y)
  2. distance_to_line(double x1, double y1, double x2, double y2, double x3, double y3)
  3. distanceAbs(final int a, final int b)
  4. distanceBase(double[] coord1, double[] coord2, int order)
  5. distanceBetween(double latitude1, double longitude1, double latitude2, double longitude2)
  6. distanceBetween(double startLatitude, double startLongitude, double endLatitude, double endLongitude)
  7. distanceBetween(double x1, double y1, double x2, double y2)
  8. distanceBetween(double x1, double y1, double x2, double y2)
  9. distanceBetween(double xpos1, double ypos1, double xpos2, double ypos2)