Calculating distance on a map isn't as simple as drawing a straight line with a ruler. Because the Earth is an oblate spheroid, the shortest distance between two points on its surface is actually a curve. This is known as the Great Circle Distance or Orthodromic distance.
The Haversine Formula
This calculator utilizes the Haversine formula, which accounts for the Earth's curvature to provide highly accurate point-to-point results. This formula is the industry standard for navigation and aviation apps.
a = sin²(Δφ/2) + cos φ1 ⋅ cos φ2 ⋅ sin²(Δλ/2)
c = 2 ⋅ atan2( √a, √(1−a) )
d = R ⋅ c
Practical Examples
Suppose you want to calculate the distance between New York City and Los Angeles:
NYC: 40.7128° N, 74.0060° W
Los Angeles: 34.0522° N, 118.2437° W
Result: Approximately 3,940 kilometers (2,448 miles).
How to use Coordinates
Decimal degrees are the standard format for GPS coordinates. Positive latitudes are North, negative are South. Positive longitudes are East, while negative longitudes are West. Ensure you include the minus sign (-) for Western or Southern coordinates to get an accurate result.
function calculateMapDistance() {
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;
if (isNaN(lat1) || isNaN(lon1) || isNaN(lat2) || isNaN(lon2)) {
alert("Please enter valid numerical coordinates for both points.");
return;
}
// Earth's radius in different units
var R;
var unitLabel;
if (unit === 'km') {
R = 6371;
unitLabel = "Kilometers";
} else if (unit === 'mi') {
R = 3958.8;
unitLabel = "Miles";
} else {
R = 3440.065;
unitLabel = "Nautical Miles";
}
// Convert degrees to radians
var dLat = (lat2 – lat1) * Math.PI / 180;
var dLon = (lon2 – lon1) * Math.PI / 180;
var rLat1 = lat1 * Math.PI / 180;
var rLat2 = lat2 * Math.PI / 180;
// Haversine calculation logic
var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(rLat1) * Math.cos(rLat2) *
Math.sin(dLon / 2) * Math.sin(dLon / 2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 – a));
var d = R * c;
// Display results
document.getElementById('distance-result').style.display = 'block';
document.getElementById('final-dist').innerHTML = d.toLocaleString(undefined, {minimumFractionDigits: 2, maximumFractionDigits: 2});
document.getElementById('unit-display').innerHTML = unitLabel;
}