This calculator determines the great-circle distance between two points on a sphere (approximating the Earth) given their latitude and longitude coordinates. The most common method used for this is the Haversine formula, which accounts for the Earth's curvature.
The Haversine Formula
The Haversine formula is used to calculate the shortest distance between two points on the surface of a sphere. It takes into account the spherical nature of the Earth, making it more accurate than simpler Euclidean distance calculations over longer distances.
The formula involves several steps:
Convert latitude and longitude from degrees to radians.
Calculate the differences in latitude and longitude: Δlat = lat2 - lat1, Δlon = lon2 - lon1.
Calculate intermediate values using the Haversine function:
a = sin²(Δlat/2) + cos(lat1) * cos(lat2) * sin²(Δlon/2)
c = 2 * atan2(√a, √(1−a))
The distance is then d = R * c, where R is the Earth's radius.
The Earth's mean radius (R) is approximately:
6371 kilometers
3958.8 miles
6,371,000 meters
20,900,000 feet
The calculator uses these radii based on the selected unit.
Use Cases for GPS Distance Calculation
Navigation: Estimating travel distances between locations for road trips, flights, or maritime travel.
Mapping Services: Powering features in apps like Google Maps or Waze to show distances and route estimations.
Logistics and Delivery: Calculating distances for delivery routes, fleet management, and optimizing delivery times.
Geographic Information Systems (GIS): Analyzing spatial data and calculating distances between geographical features.
Outdoor Activities: Planning hikes, cycling routes, or runs and estimating distances to cover.
Location-Based Services: Determining how far users are from points of interest, businesses, or other users.
function calculateDistance() {
var lat1 = parseFloat(document.getElementById("lat1").value);
var lon1 = parseFloat(document.getElementById("lon1").value);
var lat2 = parseFloat(document.getElementById("lat2").value);
var lon2 = parseFloat(document.getElementById("lon2").value);
var unit = document.getElementById("unit").value;
var resultElement = document.getElementById("result");
if (isNaN(lat1) || isNaN(lon1) || isNaN(lat2) || isNaN(lon2)) {
resultElement.textContent = "Please enter valid numeric coordinates.";
return;
}
// Earth's radius in different units
var earthRadius = {
km: 6371,
miles: 3958.8,
meters: 6371000,
feet: 20900000
};
// Convert degrees to radians
var degToRad = function(degrees) {
return degrees * (Math.PI / 180);
};
var radLat1 = degToRad(lat1);
var radLon1 = degToRad(lon1);
var radLat2 = degToRad(lat2);
var radLon2 = degToRad(lon2);
// Difference in coordinates
var dLat = radLat2 – radLat1;
var dLon = radLon2 – radLon1;
// Haversine formula
var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(radLat1) * Math.cos(radLat2) *
Math.sin(dLon / 2) * Math.sin(dLon / 2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 – a));
// Calculate distance
var distance = earthRadius[unit] * c;
resultElement.textContent = "Distance: " + distance.toFixed(2) + " " + unit;
}