Introduction
When working with geographic coordinates, calculating the distance between two points on Earth’s surface is a common task. This calculation becomes essential for applications ranging from navigation systems to location-based services. To achieve this, we use formulas that account for the spherical shape of the Earth. One such widely used formula is the Haversine formula, which calculates the great-circle distance between two points specified by their latitude and longitude.
Understanding Latitude and Longitude
Latitude and longitude are a pair of numbers that describe the position of a point on the surface of the Earth:
- Latitude measures how far north or south a point is from the equator. It ranges from -90° at the South Pole to +90° at the North Pole.
- Longitude measures how far east or west a point is from the Prime Meridian, which runs through Greenwich, England. It ranges from -180° to +180°.
These coordinates are typically expressed in decimal degrees for simplicity and precision.
The Haversine Formula
The Haversine formula calculates the shortest distance over the Earth’s surface, providing an "as-the-crow-flies" distance between two points. This distance is also known as the great-circle distance. The formula is particularly useful because it accounts for the spherical shape of the Earth and provides accurate results even for small distances.
The Haversine formula is given by:
[
a = \sin^2\left(\frac{\Delta \text{lat}}{2}\right) + \cos(\text{lat1}) \cdot \cos(\text{lat2}) \cdot \sin^2\left(\frac{\Delta \text{lon}}{2}\right)
]
[
c = 2 \cdot \text{atan2}\left(\sqrt{a}, \sqrt{1-a}\right)
]
[
d = R \cdot c
]
Where:
- ( \Delta \text{lat} ) is the difference in latitude between two points.
- ( \Delta \text{lon} ) is the difference in longitude between two points.
- ( R ) is Earth’s radius (mean radius = 6,371 km).
- ( d ) is the distance between the two points.
Implementation Examples
Below are implementations of the Haversine formula in various programming languages:
JavaScript
function getDistanceFromLatLonInKm(lat1, lon1, lat2, lon2) {
const R = 6371; // Radius of the earth in km
const dLat = deg2rad(lat2 - lat1);
const dLon = deg2rad(lon2 - lon1);
const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
Math.sin(dLon / 2) * Math.sin(dLon / 2);
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
return R * c; // Distance in km
}
function deg2rad(deg) {
return deg * (Math.PI / 180);
}
Python
from math import cos, asin, sqrt, pi
def distance(lat1, lon1, lat2, lon2):
r = 6371 # km
p = pi / 180
a = 0.5 - cos((lat2-lat1)*p)/2 + cos(lat1*p) * cos(lat2*p) * (1-cos((lon2-lon1)*p))/2
return 2 * r * asin(sqrt(a))
C#
static class DistanceAlgorithm {
const double PIx = 3.141592653589793;
const double RADIUS = 6378.16;
public static double Radians(double x) {
return x * PIx / 180;
}
public static double DistanceBetweenPlaces(
double lon1, double lat1,
double lon2, double lat2) {
double dlon = Radians(lon2 - lon1);
double dlat = Radians(lat2 - lat1);
double a = (Math.Sin(dlat / 2) * Math.Sin(dlat / 2)) +
Math.Cos(Radians(lat1)) * Math.Cos(Radians(lat2)) *
(Math.Sin(dlon / 2) * Math.Sin(dlon / 2));
double angle = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
return angle * RADIUS;
}
}
Java
public final static double AVERAGE_RADIUS_OF_EARTH_KM = 6371;
public int calculateDistanceInKilometer(double userLat, double userLng,
double venueLat, double venueLng) {
double latDistance = Math.toRadians(userLat - venueLat);
double lngDistance = Math.toRadians(userLng - venueLng);
double a = Math.sin(latDistance / 2) * Math.sin(latDistance / 2)
+ Math.cos(Math.toRadians(userLat)) * Math.cos(Math.toRadians(venueLat))
* Math.sin(lngDistance / 2) * Math.sin(lngDistance / 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
return (int)(Math.round(AVERAGE_RADIUS_OF_EARTH_KM * c));
}
Conclusion
The Haversine formula is an effective and efficient way to calculate the great-circle distance between two latitude-longitude points. It considers the Earth’s curvature, making it suitable for applications requiring high precision over short distances. By implementing this formula in various programming languages, you can integrate geospatial calculations into diverse software solutions.
Best Practices
- Always ensure that your input coordinates are in decimal degrees and convert them to radians if required by your implementation.
- Consider the radius of the Earth used (mean or equatorial) based on your application’s needs for precision.
- Test with known distances to validate the accuracy of your calculations.