Given latitude and longitude in degrees find the distance between two points on the earth.
Image Source : Wikipedia
Input : Latitude 1: 53.32055555555556 Latitude 2: 53.31861111111111 Longitude 1: -1.7297222222222221 Longitude 2: -1.6997222222222223 Output: Distance is: 2.0043678382716137 Kilometers
Problem can be solved using Haversine formula:
The great circle distance or the orthodromic distance is the shortest distance between two points on a sphere (or the surface of Earth). In order to use this method, we need to have the co-ordinates of point A and point B.The great circle method is chosen over other methods.
First, convert the latitude and longitude values from decimal degrees to radians. 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. If we want to calculate the distance between two places in kilometers, use the value 6, 378.8, which is the radius of Earth.
Find the value of the latitude in radians:
Value of Latitude in Radians, lat = Latitude / (180/pi) OR
Value of Latitude in Radians, lat = Latitude / 57.29577951
Find the value of longitude in radians:
Value of Longitude in Radians, long = Longitude / (180/pi) OR
Value of Longitude in Radians, long = Longitude / 57.29577951
Get the co-ordinates of point A in terms of latitude and longitude. Use the above conversion method to convert the values of latitude and longitude in radians. I will call it as lat1 and long1. Do the same for the co-ordinates of Point B and get lat2 and long2.
Now, to get the distance between point A and point B use the following formula:
Distance, d = 3963.0 * arccos[(sin(lat1) * sin(lat2)) + cos(lat1) * cos(lat2) * cos(long2 – long1)]
The obtained distance, d, is in miles. If you want your value to to be in units of kilometers, multiple d by 1.609344.
d in kilometers = 1.609344 * d in miles
Thus you can have the shortest distance between two places on Earth using the great circle distance approach.
- Program to calculate distance between two points in 3 D
- Program to calculate distance between two points
- Ways to choose three points with distance between the most distant points <= L
- Hammered distance between N points in a 2-D plane
- Find points at a given distance on a line of given slope
- Check whether it is possible to join two points given on circle such that distance between them is k
- Haversine formula to find distance between two points on a sphere
- Program to check if three points are collinear
- Program to check whether 4 points in a 3-D plane are Coplanar
- Program to find line passing through 2 Points
- Program to find equation of a plane passing through 3 points
- Minimum number of points to be removed to get remaining points on one side of axis
- Steps required to visit M points in order on a circular ring of N points
- Count of obtuse angles in a circle with 'k' equidistant points between 2 given points
- Prime points (Points that split a number into two primes)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.