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

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

Description

distance

License

Open Source License

Declaration

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

Method Source Code

//package com.java2s;

public class Main {
    public static double distance(double lat1, double lon1, double lat2, double lon2) {

        final int R = 6371; // Radius of the earth
        if (lat1 == 0.0 || lon1 == 0.0 || lat2 == 0.0 || lon2 == 0.0)
            return 0.0;
        double latDistance = (lat2 - lat1);
        double lonDistance = (lon2 - lon1);
        double a = Math.sin(latDistance / 2) * Math.sin(latDistance / 2)
                + Math.cos((lat1)) * Math.cos((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
        return distance;
    }/*from  w w w. j a va  2 s .c  om*/
}

Related

  1. distance(double lat1, double lng1, double lat2, double lng2)
  2. distance(double lat1, double lng1, double lat2, double lng2)
  3. distance(double lat1, double lon1, double lat2, double lon2)
  4. distance(double lat1, double lon1, double lat2, double lon2)
  5. distance(double lat1, double lon1, double lat2, double lon2)
  6. distance(double latA, double logA, double latB, double logB)
  7. distance(double latDegrees1, double lonDegrees1, double latDegrees2, double lonDegrees2)
  8. distance(double latitude1, double longitude1, double latitude2, double longitude2)
  9. distance(double lon1, double lat1, double lon2, double lat2)