Haversine distance in JavaScript: how far apart are two coordinates?

2026-11-30来源 dev.toEnglish

Say you have two GPS points and you want the distance between them. The tempting shortcut is Pythagoras on the latitude and longitude numbers. It gives wrong answers almost everywhere, and the error grows the further you get from the equator.

Why plain Pythagoras fails

One degree of latitude is about 111 km everywhere. One degree of longitude is not: it is about 111 km at the equator and shrinks with the cosine of the latitude. At 60° north, a degree of longitude is roughly half as long, so treating degrees as equal units stretches your east-west distances by about 2x.

The haversine formula

The haversine formula gives the great-circle distance between two points on a sphere. It is a few lines of JavaScript:

function haversineKm(lat1, lon1, lat2, lon2) { const R = 6371.0088; // mean Earth radius in km const toRad = (deg) => (deg * Math.PI) / 180; const dLat = toRad(lat2 - lat1); const dLon = toRad(lon2 - lon1); const a = Math.sin(dLat / 2) ** 2 + Math.cos(toRad(lat1)) * Math.cos(toRad(lat2)) * Math.sin(dLon / 2) ** 2; // Math.min guards against tiny floating point overshoot above 1 return 2 * R * Math.asin(Math.min(1, Math.sqrt(a))); } // Delhi to Mumbai: roughly 1,150 km in a straight line console.log(haversineKm(28.6139, 77.2090, 19.0760, 72.8777)); // London to Paris: roughly 340 km console.log(haversineKm(51.5074, -0.1278, 48.8566, 2.3522)); 

A route is just a sum of segments

For a path with several stops, add up the distance between each consecutive pair:

function routeKm(points) { let total = 0; for (let i = 1; i < points.length; i++) { const [lat1, lon1] = points[i - 1]; const [lat2, lon2] = points[i]; total += haversineKm(lat1, lon1, lat2, lon2); } return total; } 

Gotchas that cause most bugs

  • Order of coordinates. Most map UIs say latitude, longitude. GeoJSON says [longitude, latitude]. Swapping them gives plausible-looking but wrong numbers, so check one known pair first.
  • Degrees vs radians. Math.sin and Math.cos want radians. Forgetting the conversion is the classic mistake.
  • It is straight-line distance. Haversine is the shortest path over the surface, not a road or walking distance.
  • The Earth is not a perfect sphere. The formula assumes one, so results can differ from an ellipsoidal model by a fraction of a percent (roughly up to 0.5%). That is fine for most apps. For surveying-grade work, use an ellipsoidal method such as Vincenty or Karney's algorithm.
  • The antimeridian is fine. Because the formula uses the sine of half the longitude difference, a trip across 180° does not need special handling.

If you would rather not write code

I am the author of SnipFerry, a set of free browser tools. It has a coordinates plotting tool that calculates distances and routes from pasted points, with no sign-up: https://snipferry.com

If you need exact numbers for a legal or engineering purpose, use a proper geodesy library instead of a quick formula.

原文:https://www.a8q9.cn/en/article/haversine-distance-in-javascript-how-far-apart-are-two-coordinates-cfc7f7.html

相关文章