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

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

Description

calculates the distance in meters between two points in EPSG:4326 coodinates.

License

Open Source License

Parameter

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

Return

the distance in meters between two points in EPSG:4326 coords

Declaration

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

Method Source Code

//package com.java2s;
/*----------------------------------------------------------------------------
 This file is part of deegree, http://deegree.org/
 Copyright (C) 2001-2009 by:/* w  ww . ja va 2  s .c o  m*/
 Department of Geography, University of Bonn
 and
 lat/lon GmbH
    
 This library is free software; you can redistribute it and/or modify it under
 the terms of the GNU Lesser General Public License as published by the Free
 Software Foundation; either version 2.1 of the License, or (at your option)
 any later version.
 This library 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 Lesser General Public License for more
 details.
 You should have received a copy of the GNU Lesser General Public License
 along with this library; if not, write to the Free Software Foundation, Inc.,
 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
    
 Contact information:
    
 lat/lon GmbH
 Aennchenstr. 19, 53177 Bonn
 Germany
 http://lat-lon.de/
    
 Department of Geography, University of Bonn
 Prof. Dr. Klaus Greve
 Postfach 1147, 53001 Bonn
 Germany
 http://www.geographie.uni-bonn.de/deegree/
    
 e-mail: info@deegree.org
 ----------------------------------------------------------------------------*/

public class Main {
    /**
     * calculates the distance in meters between two points in EPSG:4326 coodinates. this is a convenience method
     * assuming the world is a ball
     * 
     * @param lon1
     * @param lat1
     * @param lon2
     * @param lat2
     * @return the distance in meters between two points in EPSG:4326 coords
     */
    public static double calcDistance(double lon1, double lat1, double lon2, double lat2) {
        double r = 6378.137;
        double rad = Math.PI / 180d;
        double cose = Math.sin(rad * lon1) * Math.sin(rad * lon2)
                + Math.cos(rad * lon1) * Math.cos(rad * lon2) * Math.cos(rad * (lat1 - lat2));
        double dist = r * Math.acos(cose) * Math.cos(rad * Math.min(lat1, lat2));

        // * 0.835 is just an heuristic correction factor
        return dist * 1000 * 0.835;
    }
}

Related

  1. CalcDistance(double lat1, double lon1, double lat2, double lon2)
  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)
  5. calcDistanceBetweenCoords(double startLat, double startLon, double endLat, double endLon)