Java Distance Calculate calculateDistanceBetween(double sourceLatitude, double sourceLongitude, double destinationLatitude, double destinationLongitude)

Here you can find the source of calculateDistanceBetween(double sourceLatitude, double sourceLongitude, double destinationLatitude, double destinationLongitude)

Description

calculate Distance Between

License

BSD License

Declaration

public static double calculateDistanceBetween(double sourceLatitude, double sourceLongitude,
            double destinationLatitude, double destinationLongitude) 

Method Source Code

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

public class Main {
    private static final double DEGREES_TO_RADIANS = Math.PI / 180.0;
    public static final double EARTH_RADIUS_IN_KILOMETERS = 6373.0;

    public static double calculateDistanceBetween(double sourceLatitude, double sourceLongitude,
            double destinationLatitude, double destinationLongitude) {
        // Keeping this as it was in DME2, though there is Math.toRadians
        double sourceLatitudeRadians = sourceLatitude * DEGREES_TO_RADIANS;
        double sourceLongitudeRadians = sourceLongitude * DEGREES_TO_RADIANS;
        double destinationLatitudeRadians = destinationLatitude * DEGREES_TO_RADIANS;
        double destinationLongitudeRadians = destinationLongitude * DEGREES_TO_RADIANS;

        double deltaLatitude = destinationLatitudeRadians - sourceLatitudeRadians;
        double deltaLongitude = destinationLongitudeRadians - sourceLongitudeRadians;

        double a = Math.pow(Math.sin(deltaLatitude / 2.0), 2.0) + Math.cos(sourceLatitudeRadians)
                * Math.cos(destinationLatitudeRadians) * Math.pow(Math.sin(deltaLongitude / 2.0), 2.0);

        double c = 2.0 * Math.atan2(Math.sqrt(a), Math.sqrt(1.0 - a));
        return c * EARTH_RADIUS_IN_KILOMETERS;
    }/*from w ww.  j a v a2s . c  o  m*/
}

Related

  1. calculateDistance(final double x1, final double y1, final double x2, final double y2)
  2. calculateDistance(float[] vec1, float[] vec2)
  3. calculateDistance(int from, int to)
  4. calculateDistance(int x1, int y1, int z1, int x2, int y2, int z2)
  5. calculateDistance(String firstString, String secondString)
  6. calculateDistanceInKm(double lat1, double lon1, double lat2, double lon2)
  7. calculateDistanceNoNormalization(float[] vec1, float[] vec2)
  8. calculateDistancePointToShowerAxis(double cogx, double cogy, double delta, double x, double y)
  9. calculateDistanceWithWGS84(double lat1, double lng1, double lat2, double lng2)