Java Utililty Methods Distance Calculate

List of utility methods to do Distance Calculate

Description

The list of methods to do Distance Calculate are organized into topic(s).

Method

doubledistanceInMetersBetween(final double lat1, final double lng1, final double lat2, final double lng2)
Calculates a distance between two "world" points.
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;
...
floatdistanceInMilesBetween(float lat1, float lng1, float lat2, float lng2)
distance In Miles Between
double earthRadius = 3958.75; 
double dLat = Math.toRadians(lat2 - lat1);
double dLng = Math.toRadians(lng2 - lng1);
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);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
float dist = (float) (earthRadius * c);
return dist;
...
FloatdistanceInMilesBetweenDouble(Float centerLat, Float centerLon, double lat, double lon)
distance In Miles Between Double
return distanceInMilesBetween(centerLat, centerLon, (float) lat, (float) lon);
doubledistanceInRadians(double latitude1, double longitude1, double latitude2, double longitude2)
distance In Radians
double x1 = Math.toRadians(latitude1);
double y1 = Math.toRadians(longitude1);
double x2 = Math.toRadians(latitude2);
double y2 = Math.toRadians(longitude2);
return Math.acos(Math.sin(x1) * Math.sin(x2) + Math.cos(x1) * Math.cos(x2) * Math.cos(y1 - y2));
doubledistanceKM(double lat1, double lng1, double lat2, double lng2)
distance KM
double dLat = Math.toRadians(lat2 - lat1);
double dLng = Math.toRadians(lng2 - lng1);
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);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
return EARTH_RADIUS * c;
intdistanceLevenshtein(CharSequence s, CharSequence t)
distance Levenshtein
if (s == null || "".equals(s)) {
    return t == null || "".equals(t) ? 0 : t.length();
} else if (t == null || "".equals(t)) {
    return s.length();
int[] v0 = new int[t.length() + 1];
int[] v1 = new int[t.length() + 1];
for (int i = 0; i < v0.length; i++) {
...
intdistanceLongitude(double latitude, double east, double west)
distance Longitude
return (int) Math
        .ceil((Math.toRadians(Math.abs(east - west)) * Math.cos(Math.toRadians(latitude)) * EARTH_RADIUS));
doubledistanceMat(double[][] m1, double[][] m2)
distance Mat
double res2 = 0;
for (int i = 0; i < m1.length; i++) {
    for (int j = 0; j < m1[i].length; j++) {
        res2 += (m1[i][j] - m2[i][j]) * (m1[i][j] - m2[i][j]);
return Math.sqrt(res2);
doubledistanceMeter(double prevLat, double prevLon, double currentLat, double currentLon)
Calculate the distance between two points (Latitude, Longitude)
double lat1Rad = Math.toRadians(prevLat);
double lat2Rad = Math.toRadians(currentLat);
double deltaLonRad = Math.toRadians(currentLon - prevLon);
return Math.acos(Math.sin(lat1Rad) * Math.sin(lat2Rad)
        + Math.cos(lat1Rad) * Math.cos(lat2Rad) * Math.cos(deltaLonRad)) * EARTH_RADIUS_METER;
doubledistanceNd(double[] p1, double[] p2, double[] scratchSpace)
distance Nd
if (p1.length != p2.length || p1.length != scratchSpace.length)
    throw new IllegalArgumentException("mismatched dimenions in distanceNd()");
if (p1 == scratchSpace || p2 == scratchSpace)
    throw new IllegalArgumentException("scratch space must be dofferent from inputs");
if (p1.length == 0)
    return 0;
for (int i = 0; i < p1.length; i++) {
    scratchSpace[i] = Math.abs(p2[i] - p1[i]);
...