turf.along
What is turf.along?
turf.along takes a GeoJSON LineString Feature and a distance, then returns a Point Feature located exactly that distance along the line. It walks segment by segment using geodesic great-circle math on the WGS84 ellipsoid, so the returned point reflects real-world distance regardless of the input's projection.
turf.along(line, distance, options?) → Feature<Point>Options include:
units—'kilometers'(default),'meters','miles','nauticalmiles','radians','degrees'
When would you use turf.along?
Use turf.along when you need to place a marker, animate a moving object, or insert a waypoint at a known distance along a route. A common browser use-case is animating a vehicle sprite on a MapLibre or Mapbox map — call turf.along for each tick of the animation with an increasing distance to interpolate the vehicle position without manually measuring segment lengths.
It is also useful in Node.js pipelines for generating evenly spaced markers on trails, mileposts along highways, or sampling points for elevation lookups. If you need to chunk an entire line into equal segments, pair it with turf.length and a loop, or use turf.lineChunk.
undefinedFAQs
What happens if the distance exceeds the line length?
turf.along returns the last coordinate of the line. It does not extrapolate past the endpoint. If you need to detect this case, compare your distance against turf.length(line) before calling along.
How do I install just this function?
Install the scoped module for smaller bundles: npm install @turf/along. You can also use the full bundle @turf/turf from npm or a CDN, but the scoped package tree-shakes far better in modern bundlers like Vite or Webpack 5.
Does it work with MultiLineString or just LineString?
turf.along only accepts a single LineString Feature. For MultiLineString, flatten it first with turf.flatten and call along on whichever line segment is relevant, or concatenate coordinates into one line if the parts are contiguous.
Is the interpolation linear or geodesic between vertices?
Distances between vertices are computed geodetically using haversine, but the returned point is placed along the straight segment between the two nearest vertices. For smoothly curved output, densify the line first with turf.lineChunk or use turf.greatCircle for long-haul routes.