Returns the calculation of the geographical distance between two points. - Java java.lang

Java examples for java.lang:Math Algorithm

Description

Returns the calculation of the geographical distance between two points.

Demo Code


import java.awt.geom.Point2D;
import java.awt.geom.Point2D.Double;

public class Main{
    public static void main(String[] argv) throws Exception{
        double pointA = 2.45678;
        double pointB = 2.45678;
        System.out.println(distanceTwoGeoPoint(pointA,pointB));
    }/* ww  w  . j a  v  a 2s . c  o  m*/
    private static final double RADIUS = 6371.5;
    /**
     * Returns the calculation of the geographical distance between two points.
     * @param pointA the point from start the segment. 
     * @param pointB the point from ends the segment.
     * @return the distance between two points.
     */
    public static double distanceTwoGeoPoint(Double pointA, Double pointB) {
        double latA = Math.toRadians(pointA.getX());
        double latB = Math.toRadians(pointB.getX());
        double dLon = Math.abs(Math.toRadians(pointA.getY())
                - Math.toRadians(pointB.getY()));
        return (RADIUS * (Math.acos(Math.sin(latA) * Math.sin(latB)
                + Math.cos(latA) * Math.cos(latB) * Math.cos(dLon))));
    }
}

Related Tutorials