Java Distance Calculate computeDistance(Double startLat, Double startLon, Double endLat, Double endLon)

Here you can find the source of computeDistance(Double startLat, Double startLon, Double endLat, Double endLon)

Description

computes the distance between 2 points

License

Open Source License

Parameter

Parameter Description
startLat a parameter
startLon a parameter
endLat a parameter
endLon a parameter

Declaration

public static Double computeDistance(Double startLat, Double startLon, Double endLat, Double endLon) 

Method Source Code

//package com.java2s;
/*/*from w  w w  . j  av  a 2  s . co m*/
 *  Copyright (C) 2010-2012 Stichting Akvo (Akvo Foundation)
 *
 *  This file is part of Akvo FLOW.
 *
 *  Akvo FLOW is free software: you can redistribute it and modify it under the terms of
 *  the GNU Affero General Public License (AGPL) as published by the Free Software Foundation,
 *  either version 3 of the License or any later version.
 *
 *  Akvo FLOW 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 Affero General Public License included below for more details.
 *
 *  The full license text can also be seen at <http://www.gnu.org/licenses/agpl.html>.
 */

public class Main {
    public static final double EARTH_RADIUS = 6371.0;

    /**
     * computes the distance between 2 points
     * 
     * @param startLat
     * @param startLon
     * @param endLat
     * @param endLon
     * @return
     */
    public static Double computeDistance(Double startLat, Double startLon, Double endLat, Double endLon) {
        Double distance = null;

        double p1 = Math.cos(startLat) * Math.cos(startLon) * Math.cos(endLat) * Math.cos(endLat);
        double p2 = Math.cos(startLat) * Math.sin(startLon) * Math.cos(endLat) * Math.sin(endLon);
        double p3 = Math.sin(startLat) * Math.sin(endLat);

        distance = (Math.acos(p1 + p2 + p3) * EARTH_RADIUS);
        // Return distance in meters
        distance = distance * 1000;
        return distance;

    }
}

Related

  1. calculateDistanceInKm(double lat1, double lon1, double lat2, double lon2)
  2. calculateDistanceNoNormalization(float[] vec1, float[] vec2)
  3. calculateDistancePointToShowerAxis(double cogx, double cogy, double delta, double x, double y)
  4. calculateDistanceWithWGS84(double lat1, double lng1, double lat2, double lng2)
  5. computeDistance(double lat1, double lon1, double lat2, double lon2)
  6. computeDistance(final int[] position1, final int[] position2)
  7. computeDistance(int p1x, int p1y, int p2x, int p2y)
  8. computeDistance(long[] a, long[] b)
  9. computeDistanceAndBearingFast(double lat1, double lon1, double lat2, double lon2, float[] results)