Java Distance Calculate calculateDistanceWithWGS84(double lat1, double lng1, double lat2, double lng2)

Here you can find the source of calculateDistanceWithWGS84(double lat1, double lng1, double lat2, double lng2)

Description

calculate Distance With WGS

License

Open Source License

Declaration

public static double calculateDistanceWithWGS84(double lat1, double lng1, double lat2, double lng2) 

Method Source Code

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

public class Main {

    public static double calculateDistanceWithWGS84(double lat1, double lng1, double lat2, double lng2) {

        return calculateDistance(toDegree(lat1), toDegree(lng1), toDegree(lat2), toDegree(lng2));

        //        return distVincenty(toDegree(lat1), toDegree(lng1), toDegree(lat2), toDegree(lng2));
    }/*w w  w .  j  a  v a 2 s . co  m*/

    public static double calculateDistance(double lat1, double lng1, double lat2, double lng2) {
        double earthRadius = 6371393.0;
        double dLat = Math.toRadians(lat2 - lat1);
        double dLng = Math.toRadians(lng2 - lng1);
        double sindLat = Math.sin(dLat / 2);
        double sindLng = Math.sin(dLng / 2);
        double a = Math.pow(sindLat, 2)
                + Math.pow(sindLng, 2) * Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2));
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
        double dist = earthRadius * c;

        return dist;
    }

    private static double toDegree(double wgs84) {
        long degree = Math.round(wgs84) / 100;
        double decimal = (wgs84 - degree * 100) / 60.0;
        return degree + decimal;
    }
}

Related

  1. calculateDistance(String firstString, String secondString)
  2. calculateDistanceBetween(double sourceLatitude, double sourceLongitude, double destinationLatitude, double destinationLongitude)
  3. calculateDistanceInKm(double lat1, double lon1, double lat2, double lon2)
  4. calculateDistanceNoNormalization(float[] vec1, float[] vec2)
  5. calculateDistancePointToShowerAxis(double cogx, double cogy, double delta, double x, double y)
  6. computeDistance(double lat1, double lon1, double lat2, double lon2)
  7. computeDistance(Double startLat, Double startLon, Double endLat, Double endLon)
  8. computeDistance(final int[] position1, final int[] position2)
  9. computeDistance(int p1x, int p1y, int p2x, int p2y)