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

doubledistance(double lat1, double lng1, double lat2, double lng2)
Calculate distance (in meters) between two points in (latitude, longitude).
if (Math.abs(lat1) == LATITUDE_MAX || Math.abs(lat2) == LATITUDE_MAX || Math.abs(lng1) == LONGITUDE_MAX
        || Math.abs(lng2) == LONGITUDE_MAX) {
    return Double.MAX_VALUE;
final int R = 6371;
Double dLat = Math.toRadians(lat2 - lat1);
Double dLon = Math.toRadians(lng2 - lng1);
double sinLatDist = Math.sin(dLat / 2);
...
doubledistance(double lat1, double lng1, double lat2, double lng2)
distance
return distance(lat1, lng1, 0, lat2, lng2, 0);
doubledistance(double lat1, double lon1, double lat2, double lon2)
distance
double p1lat = Math.toRadians(lat1);
double p1lon = Math.toRadians(lon1);
double p2lat = Math.toRadians(lat2);
double p2lon = Math.toRadians(lon2);
return EARTH_RADIUS * Math.acos(makeDoubleInRange(
        Math.sin(p1lat) * Math.sin(p2lat) + Math.cos(p1lat) * Math.cos(p2lat) * Math.cos(p2lon - p1lon)));
doubledistance(double lat1, double lon1, double lat2, double lon2)
Calculate distance between two points in latitude and longitude taking into account height difference.
final int R = 6371; 
Double latDistance = deg2rad(lat2 - lat1);
Double lonDistance = deg2rad(lon2 - lon1);
Double a = Math.sin(latDistance / 2) * Math.sin(latDistance / 2) + Math.cos(deg2rad(lat1))
        * Math.cos(deg2rad(lat2)) * Math.sin(lonDistance / 2) * Math.sin(lonDistance / 2);
Double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
double distance = R * c * 1000; 
return distance;
...
doubledistance(double lat1, double lon1, double lat2, double lon2)
Calculate 2 point distances, in meter
double dLat = Math.toRadians(lat2 - lat1);
double dLng = Math.toRadians(lon2 - lon1);
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));
double dist = earthRadius * c;
return new Double(dist).doubleValue();
doubledistance(double lat1, double lon1, double lat2, double lon2)
distance
final int R = 6371; 
if (lat1 == 0.0 || lon1 == 0.0 || lat2 == 0.0 || lon2 == 0.0)
    return 0.0;
double latDistance = (lat2 - lat1);
double lonDistance = (lon2 - lon1);
double a = Math.sin(latDistance / 2) * Math.sin(latDistance / 2)
        + Math.cos((lat1)) * Math.cos((lat2)) * Math.sin(lonDistance / 2) * Math.sin(lonDistance / 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
...
doubledistance(double latA, double logA, double latB, double logB)
distance
double earthR = 6371000;
double x = Math.cos(latA * Math.PI / 180) * Math.cos(latB * Math.PI / 180)
        * Math.cos((logA - logB) * Math.PI / 180);
double y = Math.sin(latA * Math.PI / 180) * Math.sin(latB * Math.PI / 180);
double s = x + y;
if (s > 1)
    s = 1;
if (s < -1)
...
doubledistance(double latDegrees1, double lonDegrees1, double latDegrees2, double lonDegrees2)
distance
double[] offsets = WGS84displacement(latDegrees1, lonDegrees1, 0, latDegrees2, lonDegrees2, 0);
return Math.hypot(offsets[0], offsets[1]);
doubledistance(double latitude1, double longitude1, double latitude2, double longitude2)
distance
return Math.sqrt((latitude1 - latitude2) * (latitude1 - latitude2)
        + (longitude1 - longitude2) * (longitude1 - longitude2));
doubledistance(double lon1, double lat1, double lon2, double lat2)
distance
double radLat1 = rad(lat1);
double radLat2 = rad(lat2);
double a = radLat1 - radLat2;
double b = rad(lon1) - rad(lon2);
double c = 2.0D * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2.0D), 2.0D)
        + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2.0D), 2.0D)));
return c * 6371004.0D;