Skip to content

distance

Great-circle distance between two WGS84 points, in kilometres (haversine formula, mean Earth radius of 6371 km). The Earth is treated as a sphere, so the result can be off by up to about 0.5% compared to an ellipsoidal model — plenty for “how far is it” and radius checks. Near antipodal points the formula loses a little precision (well under a metre).

Available since v3.1.8

import { distance } from '@helpers4/geography';
// or, from the all-in-one package (same code, one install):
import { distance } from 'helpers4/geography';
distance(lat1: number, lon1: number, lat2: number, lon2: number): number
ParameterTypeDescription
lat1numberLatitude of the first point, in degrees
lon1numberLongitude of the first point, in degrees
lat2numberLatitude of the second point, in degrees
lon2numberLongitude of the second point, in degrees

number — The distance in kilometres

Great-circle distance in kilometres between two latitude/longitude points.

distance(48.8566, 2.3522, 51.5074, -0.1278)
// => about 343.6 (Paris to London)

A point is at distance 0 from itself.

distance(10, 20, 10, 20) // => 0

View source on GitHub