Java Distance Calculate calcDistanceBetweenCoords(double startLat, double startLon, double endLat, double endLon)

Here you can find the source of calcDistanceBetweenCoords(double startLat, double startLon, double endLat, double endLon)

Description

Calculates the distance in meter between to coordinates.

License

Open Source License

Parameter

Parameter Description
startNode Node where the way begins.
endNode Node where the way ends.

Return

Distance in meters.

Declaration

public static double calcDistanceBetweenCoords(double startLat, double startLon, double endLat, double endLon) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/*  ww  w.  ja v a2s.c o m*/
     * Calculates the distance in meter between to coordinates. The formula is
     * derived from "Seitencosinussatz".
     * 
     * @param startNode
     *            Node where the way begins.
     * @param endNode
     *            Node where the way ends.
     * @return Distance in meters.
     */
    public static double calcDistanceBetweenCoords(double startLat, double startLon, double endLat, double endLon) {

        if (startLat == endLat && startLon == endLon) {
            return 0;
        }

        return Math.acos(Math.sin(endLat / 180. * Math.PI) * Math.sin(startLat / 180. * Math.PI)
                + Math.cos(endLat / 180. * Math.PI) * Math.cos(startLat / 180. * Math.PI)
                        * Math.cos(endLon / 180. * Math.PI - startLon / 180. * Math.PI))
                * 6380. // ca.
                // der
                // Erdradius
                * 1000; // Umrechnung von km in m
    }
}

Related

  1. CalcDistance(double lat1, double lon1, double lat2, double lon2)
  2. calcDistance(double lon1, double lat1, double lon2, double lat2)
  3. calcDistance(final int coordinate1, final int coordinate2)
  4. calcDistance(int ax, int ay, int bx, int by)
  5. calcDistance(int x1, int y1, int x2, int y2)
  6. calcDistanceHubery(double lat1, double lng1, double lat2, double lng2, int type)
  7. calcDistanceIfAccel(double startVelocity, double accel, double targetVelocity, double duration)
  8. calcDistanceToStop(double startingVelocity, double maxDeceleration)
  9. calculateDistance(double lat1, double lng1, double lat2, double lng2)