Java Distance Calculate CalcDistance(double lat1, double lon1, double lat2, double lon2)

Here you can find the source of CalcDistance(double lat1, double lon1, double lat2, double lon2)

Description

Calculate the distance between two GPS points (without the elevation)

License

Open Source License

Parameter

Parameter Description
lat1 Latitude of the first point
lon1 Longitude of the first point
lat2 Latitude of the second point
lon2 Longitude of the second point

Return

Distance in meter

Declaration

public static double CalcDistance(double lat1, double lon1, double lat2, double lon2) 

Method Source Code

//package com.java2s;
/*// w  w  w  . j a v a  2 s .co m
 * Course Generator
 * Copyright (C) 2016 Pierre Delore
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Calculate the distance between two GPS points (without the elevation)
     * 
     * @param lat1
     *            Latitude of the first point
     * @param lon1
     *            Longitude of the first point
     * @param lat2
     *            Latitude of the second point
     * @param lon2
     *            Longitude of the second point
     * @return Distance in meter
     */
    public static double CalcDistance(double lat1, double lon1, double lat2, double lon2) {
        double a, c, dDistance, dLat1InRad, dLong1InRad, dLat2InRad, dLong2InRad, dLongitude, dLatitude;
        double kEarthRadiusKms;

        kEarthRadiusKms = 6378.14; // 6376.5

        dDistance = 0; // Double.MinValue
        dLat1InRad = lat1 * (Math.PI / 180.0);
        dLong1InRad = lon1 * (Math.PI / 180.0);
        dLat2InRad = lat2 * (Math.PI / 180.0);
        dLong2InRad = lon2 * (Math.PI / 180.0);

        dLongitude = dLong2InRad - dLong1InRad;
        dLatitude = dLat2InRad - dLat1InRad;

        // Intermediate result a.
        a = Math.pow(Math.sin(dLatitude / 2.0), 2.0)
                + Math.cos(dLat1InRad) * Math.cos(dLat2InRad) * Math.pow(Math.sin(dLongitude / 2.0), 2.0);

        // Intermediate result c (great circle distance in Radians)
        c = 2.0 * Math.atan2(Math.sqrt(a), Math.sqrt(1.0 - a));

        // Distance.
        dDistance = kEarthRadiusKms * c;

        // Result in meter
        return dDistance * 1000.0;
    }
}

Related

  1. calcDistance(double lon1, double lat1, double lon2, double lat2)
  2. calcDistance(final int coordinate1, final int coordinate2)
  3. calcDistance(int ax, int ay, int bx, int by)
  4. calcDistance(int x1, int y1, int x2, int y2)