Jake Smith
Jake Smith

Reputation: 630

How to get distance between two coordinates with Mapbox

I am learning how to use the Mapbox API for my node app and I want to perform calculations on the backend like getting the distance between two coordinates.

How can I achieve this?, the Mapbox documentation doesn't make this clear...

Set-up the SDK

const mapboxSdk = require("mapbox")
let mapbox = new mapboxSdk(process.env.MAPBOX_TOKEN)

// EXAMPLE OF WHAT I WANT TO DO

coordA = {lat : 33.968123, long: -118.419454}

coordB = {{lat : 33.997223, long: -117.929145}}

const distance = await mapbox.getDistance(coordA, coordB)

console.log(distance)

Upvotes: 2

Views: 4792

Answers (1)

Declan McKelvey-Hembree
Declan McKelvey-Hembree

Reputation: 1180

I believe Mapbox recommends using the Turf.js library for this type of thing: Turf.js Distance Function. However, the distance between two lat/long coords is a simple geometry problem that doesn't need any external APIs/libraries, and can be calculated from your coordinates using the Haversine Formula. Check this SO question for implementations of the Haversine formula as a javascript function.

Upvotes: 3

Related Questions