Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating distance between two points, using latitude longitude?

Here's my try, it's just a snippet of my code:

final double RADIUS = 6371.01; double temp = Math.cos(Math.toRadians(latA))             * Math.cos(Math.toRadians(latB))             * Math.cos(Math.toRadians((latB) - (latA)))             + Math.sin(Math.toRadians(latA))             * Math.sin(Math.toRadians(latB));     return temp * RADIUS * Math.PI / 180; 

I am using this formulae to get the latitude and longitude:

x = Deg + (Min + Sec / 60) / 60) 
like image 686
m4design Avatar asked Sep 12 '10 09:09

m4design


People also ask

How do you find the distance between two points using latitude and longitude?

For this divide the values of longitude and latitude of both the points by 180/pi. The value of pi is 22/7. The value of 180/pi is approximately 57.29577951. If we want to calculate the distance between two places in miles, use the value 3, 963, which is the radius of Earth.

How do you calculate distance using latitude?

Calculating the distance between latitude lines is easy because this distance never varies. If you treat the Earth as a sphere with a circumference of 25,000 miles, then one degree of latitude is 25,000/360 = 69.44 miles.

How do I calculate distance using latitude and longitude on Google Maps?

The formula for calculating longitude distance is: "Dep = d. long * Cos Mid. Lat" Dep is the same thing as miles.


2 Answers

The Java code given by Dommer above gives slightly incorrect results but the small errors add up if you are processing say a GPS track. Here is an implementation of the Haversine method in Java which also takes into account height differences between two points.

/**  * Calculate distance between two points in latitude and longitude taking  * into account height difference. If you are not interested in height  * difference pass 0.0. Uses Haversine method as its base.  *   * lat1, lon1 Start point lat2, lon2 End point el1 Start altitude in meters  * el2 End altitude in meters  * @returns Distance in Meters  */ public static double distance(double lat1, double lat2, double lon1,         double lon2, double el1, double el2) {      final int R = 6371; // Radius of the earth      double latDistance = Math.toRadians(lat2 - lat1);     double lonDistance = Math.toRadians(lon2 - lon1);     double a = Math.sin(latDistance / 2) * Math.sin(latDistance / 2)             + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(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; // convert to meters      double height = el1 - el2;      distance = Math.pow(distance, 2) + Math.pow(height, 2);      return Math.sqrt(distance); } 
like image 177
David George Avatar answered Sep 29 '22 13:09

David George


Here's a Java function that calculates the distance between two lat/long points, posted below, just in case it disappears again.

    private double distance(double lat1, double lon1, double lat2, double lon2, char unit) {       double theta = lon1 - lon2;       double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));       dist = Math.acos(dist);       dist = rad2deg(dist);       dist = dist * 60 * 1.1515;       if (unit == 'K') {         dist = dist * 1.609344;       } else if (unit == 'N') {         dist = dist * 0.8684;         }       return (dist);     }          /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/     /*::  This function converts decimal degrees to radians             :*/     /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/     private double deg2rad(double deg) {       return (deg * Math.PI / 180.0);     }          /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/     /*::  This function converts radians to decimal degrees             :*/     /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/     private double rad2deg(double rad) {       return (rad * 180.0 / Math.PI);     }          System.out.println(distance(32.9697, -96.80322, 29.46786, -98.53506, 'M') + " Miles\n");     System.out.println(distance(32.9697, -96.80322, 29.46786, -98.53506, 'K') + " Kilometers\n");     System.out.println(distance(32.9697, -96.80322, 29.46786, -98.53506, 'N') + " Nautical Miles\n"); 
like image 33
dommer Avatar answered Sep 29 '22 14:09

dommer