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

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

Description

calc distance between two points, using spherical earth (ignoring ellipsoidal effects) calculation.

License

Open Source License

Parameter

Parameter Description
lat1 a parameter
lon1 a parameter
lat2 a parameter
lon2 a parameter

Declaration

public static final double distSphericalEarth(double lat1, double lon1, double lat2, double lon2) 

Method Source Code

//package com.java2s;
/*/*from  w  ww  . j a  v  a 2 s. c om*/
 * Copyright (c) 2011 Tal Shalif
 * 
 * This file is part of Talos-Rowing.
 * 
 * Talos-Rowing 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.
 * 
 * Talos-Rowing 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 Talos-Rowing.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    private static final double R = 6371;

    /**
     * calc distance between two points, using spherical earth (ignoring ellipsoidal effects) calculation. 
     * @see http://www.movable-type.co.uk/scripts/latlong.html
     * @param lat1
     * @param lon1
     * @param lat2
     * @param lon2
     * @return
     */
    public static final double distSphericalEarth(double lat1, double lon1, double lat2, double lon2) {

        double dLat = Math.toRadians(lat2 - lat1);
        double dLon = Math.toRadians(lon2 - lon1);
        double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(Math.toRadians(lat1))
                * Math.cos(Math.toRadians(lat2)) * Math.sin(dLon / 2) * Math.sin(dLon / 2);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
        double d = R * c;

        return d * 1000;
    }
}

Related

  1. distPointLineSigned(double px, double py, double qx, double qy, double rx, double ry)
  2. distPointLineSquared(double px, double py, double qx, double qy, double rx, double ry)
  3. distPointSegmentSquared(double px, double py, double qx, double qy, double rx, double ry)
  4. distribution(int[] values)
  5. distsimFeaturesException()
  6. distSq(double x1, double y1, double x2, double y2)
  7. distSq(double x1, double y1, double z1, double x2, double y2, double z2)
  8. distSqr2(double x1, double y1, double x2, double y2)
  9. distSquared(double ax, double ay, double bx, double by)