如何使用 Mapbox 获取两个坐标之间的距离

How to get distance between two coordinates with Mapbox

我正在学习如何将 Mapbox API 用于我的节点应用程序,我想在后端执行计算,例如获取两个坐标之间的距离。

我怎样才能做到这一点?Mapbox 文档没有说清楚...

设置 SDK

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

// 我想做的事的例子

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

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

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

console.log(distance)

我相信 Mapbox 推荐使用 Turf.js 库来处理这种类型的事情: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 将 Haversine 公式作为 javascript 函数来实现。