1

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)
Jake Smith
  • 580
  • 3
  • 11

1 Answers1

2

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.