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

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

Description

calculate Distance In Km

License

Apache License

Declaration

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

Method Source Code

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

public class Main {
    public static double calculateDistanceInKm(double lat1, double lon1, double lat2, double lon2) {
        int R = 6371; // Radius of the earth in km
        double dLat = deg2rad(lat2 - lat1); // deg2rad below
        double dLon = deg2rad(lon2 - lon1);
        double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
                + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.sin(dLon / 2) * Math.sin(dLon / 2);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
        double d = R * c; // Distance in km
        return d;
    }//from   w  w w  . j  av a 2 s.c o m

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

Related

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