Java Distance Calculate distanceHaversine(Double lat1, Double lon1, Double lat2, Double lon2, String param)

Here you can find the source of distanceHaversine(Double lat1, Double lon1, Double lat2, Double lon2, String param)

Description

distance Haversine

License

Apache License

Declaration

public static Double distanceHaversine(Double lat1, Double lon1, Double lat2, Double lon2, String param) 

Method Source Code

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

public class Main {
    public static Double distanceHaversine(Double lat1, Double lon1, Double lat2, Double lon2, String param) {

        final Double earthRadius = 3960.00;
        Double latDistance = lat2 - lat1;
        Double lonDistance = lon2 - lon1;

        Double a = Math.sin(latDistance / 2) * Math.sin(latDistance / 2) + Math.cos(toRad(lat1))
                * Math.cos(toRad(lat2)) * Math.sin(lonDistance / 2) * Math.sin(lonDistance / 2);
        Double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
        Double distance = earthRadius * c;

        if (param.equals("miles"))
            return distance;
        else if (param.equals("km"))
            return distance * 1.609344;
        else//from ww  w .  jav a 2 s  .  co m
            return (distance * 1.609344) / 1000;

    }

    private static Double toRad(Double value) {
        return value * Math.PI / 180;
    }
}

Related

  1. distanceFast(double lat1, double lon1, double lat2, double lon2)
  2. distanceFrom(double lat1, double long1, double lat2, double long2)
  3. DistanceFromLine(float cx, float cy, float ax, float ay, float bx, float by)
  4. distanceFromLineToPoint(double x1, double y1, double z1, double x2, double y2, double z2, double px, double py, double pz)
  5. distanceFromZero(int x, int y)
  6. distanceInf(double[] p1, double[] p2)
  7. distanceInMeters(double lat1, double lon1, double lat2, double lon2)
  8. distanceInMetersBetween(final double lat1, final double lng1, final double lat2, final double lng2)
  9. distanceInMilesBetween(float lat1, float lng1, float lat2, float lng2)