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 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.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Program to calculate distance between two points
- Program to calculate distance between two points in 3 D
- 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
- Find the maximum possible distance from origin using given points
- Find integral points with minimum distance from given set of integers using BFS
- Find the integer points (x, y) with Manhattan distance atleast N
- Haversine formula to find distance between two points on a sphere
- Sort an Array of Points by their distance from a reference Point
- Distance between end points of Hour and minute hand at given time
- 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
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.