get Distance In Radians - Java java.lang

Java examples for java.lang:Math Geometry Distance

Description

get Distance In Radians

Demo Code


//package com.java2s;

public class Main {
    /**//from   w  w  w  . ja  va2 s  .  co  m
     * 
     * @param sourceLongitude
     * @param sourceLatitude
     * @param destLongitude
     * @param destLatitude
     * @return distance in radians between points
     */
    public static double getDistanceInRadians(double sourceLongitude,
            double sourceLatitude, double destLongitude, double destLatitude) {
        // Need to convert from degrees to radians first
        double long1 = Math.toRadians(sourceLongitude);
        double lat1 = Math.toRadians(sourceLatitude);
        double long2 = Math.toRadians(destLongitude);
        double lat2 = Math.toRadians(destLatitude);

        // Now we can get the distance in radians
        // https://en.wikipedia.org/wiki/Great-circle_distance#Formulas
        double distanceRadians = Math.acos(Math.sin(lat1) * Math.sin(lat2)
                + Math.cos(lat1) * Math.cos(lat2)
                * Math.cos(Math.abs(long1 - long2)));

        return distanceRadians;
    }
}

Related Tutorials