Java Distance Calculate distanceTo(double latitudeGiven, double longitudeGiven, double latitudeTaken, double longitudeTaken)

Here you can find the source of distanceTo(double latitudeGiven, double longitudeGiven, double latitudeTaken, double longitudeTaken)

Description

Compares latitudes and longitudes.

License

Apache License

Parameter

Parameter Description
latitudeGiven Latitude given by client.
longitudeGiven Longitude given by client.
latitudeTaken Latitude fetched from DB.
longitudeTaken Longitude fetched from DB.

Return

Distance in miles.

Declaration

public static double distanceTo(double latitudeGiven, double longitudeGiven, double latitudeTaken,
        double longitudeTaken) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**/* www .  j  a  v a2s  . co m*/
     * Compares latitudes and longitudes.
     * 
     * @param latitudeGiven Latitude given by client.
     * @param longitudeGiven Longitude given by client.
     * @param latitudeTaken Latitude fetched from DB.
     * @param longitudeTaken Longitude fetched from DB.
     * 
     * @return {@link Double} Distance in miles.
     */
    public static double distanceTo(double latitudeGiven, double longitudeGiven, double latitudeTaken,
            double longitudeTaken) {
        double STATUTE_MILES_PER_NAUTICAL_MILE = 1.15077945;
        double lat1 = Math.toRadians(latitudeGiven);
        double lon1 = Math.toRadians(longitudeGiven);
        double lat2 = Math.toRadians(latitudeTaken);
        double lon2 = Math.toRadians(longitudeTaken);

        // great circle distance in radians, using law of cosines formula
        double angle = Math
                .acos(Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1) * Math.cos(lat2) * Math.cos(lon1 - lon2));

        // each degree on a great circle of Earth is 60 nautical miles
        double nauticalMiles = 60 * Math.toDegrees(angle);
        double statuteMiles = STATUTE_MILES_PER_NAUTICAL_MILE * nauticalMiles;
        return statuteMiles;
    }
}

Related

  1. distanceSquared(int[] referenceBlock, float[] center)
  2. distanceSquaredPointToLine(float px, float py, float x1, float y1, float x2, float y2)
  3. distanceSquaredPointToPoint(float x1, float y1, float x2, float y2)
  4. distanceSqured(int x, int y, int z, int x1, int y1, int z1)
  5. distanceSV(double[] sv1, double[] sv2)
  6. distanceTo(final int x, final int y, final int thatx, final int thaty)
  7. distanceToAncestor(Class entity, Class ancestor)
  8. distanceToClass(Class theClass, Class theAncestor)
  9. distanceToInterface(Class theClass, Class theInterface)