Turf.jsMeasurement

turf.distance

What is turf.distance?

turf.distance returns the geodesic distance between two Point features using the haversine formula. The result is a number in the requested units — kilometers by default. It is accurate enough for almost all consumer mapping use-cases and is the fastest way to measure point-to-point distance in browser or Node.js code.

JavaScript
turf.distance(from, to, options?) → number

Options include:

  • units'kilometers' (default), 'meters', 'miles', 'nauticalmiles', 'radians', 'degrees'

When would you use turf.distance?

Use turf.distance for any point-to-point measurement — showing how far a pin is from a user's location, filtering POIs within N kilometers, or computing speed from successive GPS fixes. It is also a building block for nearest-neighbour lookups when you do not need a full spatial index.

In Node.js ETL, turf.distance is ideal for sanity-checking imported coordinates ("is this address really near the geocoded match?") or for computing simple distance attributes during feature hydration.

JavaScript
import distance from '@turf/distance';

FAQs

How accurate is haversine versus Vincenty?

Haversine treats the Earth as a perfect sphere (radius 6371 km) and is accurate to within ~0.5% for most distances. If you need sub-metre accuracy across thousands of kilometres, use a Vincenty or geographiclib implementation instead; for UI and analytical work, turf.distance is almost always sufficient.

What does the `'degrees'` unit mean?

It returns the central angle in degrees subtended at Earth's centre between the two points. This is useful for algorithms working in angular units but rarely for user-facing distance display.

How do I install just distance?

npm install @turf/distance. It depends only on @turf/helpers and @turf/invariant, so it is a lightweight pick for interactive mapping apps.

How do I measure the length of a whole line?

Use turf.length for a LineString or MultiLineString — it sums haversine distances segment by segment. For point-to-line distance, use turf.pointToLineDistance.