Java Distance Calculate distanceInMetersBetween(final double lat1, final double lng1, final double lat2, final double lng2)

Here you can find the source of distanceInMetersBetween(final double lat1, final double lng1, final double lat2, final double lng2)

Description

Calculates a distance between two "world" points.

License

Open Source License

Parameter

Parameter Description
lat1 a parameter
lng1 a parameter
lat2 a parameter
lng2 a parameter

Declaration

private static double distanceInMetersBetween(final double lat1, final double lng1, final double lat2,
        final double lng2) 

Method Source Code

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

public class Main {
    /**/*w  ww  .  j  av a2 s.c  o m*/
     * Calculates a distance between two "world" points.
     * 
     * @see http://stackoverflow.com/questions/837872/calculate-distance-in-meters-when-you-know-longitude-and-latitude-in-java
     * 
     * @param lat1
     * @param lng1
     * @param lat2
     * @param lng2
     * @return
     */
    private static double distanceInMetersBetween(final double lat1, final double lng1, final double lat2,
            final double lng2) {
        final double earthRadius = 3958.75;
        final double dLat = Math.toRadians(lat2 - lat1);
        final double dLng = Math.toRadians(lng2 - lng1);
        final double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(Math.toRadians(lat1))
                * Math.cos(Math.toRadians(lat2)) * Math.sin(dLng / 2) * Math.sin(dLng / 2);
        final double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
        final double dist = earthRadius * c;

        final int meterConversion = 1609;

        return dist * meterConversion;
    }
}

Related

  1. distanceFromLineToPoint(double x1, double y1, double z1, double x2, double y2, double z2, double px, double py, double pz)
  2. distanceFromZero(int x, int y)
  3. distanceHaversine(Double lat1, Double lon1, Double lat2, Double lon2, String param)
  4. distanceInf(double[] p1, double[] p2)
  5. distanceInMeters(double lat1, double lon1, double lat2, double lon2)
  6. distanceInMilesBetween(float lat1, float lng1, float lat2, float lng2)
  7. distanceInMilesBetweenDouble(Float centerLat, Float centerLon, double lat, double lon)
  8. distanceInRadians(double latitude1, double longitude1, double latitude2, double longitude2)
  9. distanceKM(double lat1, double lng1, double lat2, double lng2)