Approximate the distance between to coordinates in meters. This method is an approximation only, it does NOT calculate the real arc length - Java java.lang

Java examples for java.lang:Math Geometry Distance

Description

Approximate the distance between to coordinates in meters. This method is an approximation only, it does NOT calculate the real arc length

Demo Code



public class Main{
    /**// w  w w  .  jav a  2s  . com
     * Approximate the distance between to coordinates in meters.<br>
     * <br>
     * 
     * <b>This method is an approximation only, it does NOT calculate the real
     * arc length</b>
     * 
     * @param c1
     * @param c2
     * @return
     */
    public static double getDistanceMetersApprox(final Coordinates c1,
            final Coordinates c2) {
        final double R = 6378.137;

        double dLong = (c1.getLongtiude() - c2.getLongtiude()) * Math.PI
                / 180.0;
        double dLat = (c1.getLatitude() - c2.getLatitude()) * Math.PI
                / 180.0;

        double a = Math.sin(dLong / 2) * Math.sin(dLat / 2)
                + Math.cos(c1.getLatitude() * Math.PI / 180.0)
                * Math.cos(c2.getLatitude() * Math.PI / 180.0);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
        double d = R * c;
        return d * 1000.0;
    }
}

Related Tutorials