Node.js 谷歌地图方向API:计算给定点是否位于路线上

Node.js 谷歌地图方向API:计算给定点是否位于路线上,node.js,google-maps,google-maps-api-3,Node.js,Google Maps,Google Maps Api 3,计算给定点是否位于路径上,以下是我迄今为止尝试的: const googleMapsClient = require('@google/maps').createClient({ key: 'KEY' }); googleMapsClient.directions({ origin: 'Bangalore, Karnataka', destination: 'Mumbai, Maharathra', mode: "driving", }, function(e

计算给定点是否位于路径上,以下是我迄今为止尝试的:

const googleMapsClient  = require('@google/maps').createClient({
    key: 'KEY'
});

googleMapsClient.directions({
    origin: 'Bangalore, Karnataka',
    destination: 'Mumbai, Maharathra',
    mode: "driving",
}, function(err, response) {
    if (!err) {
        console.log(response);
    }
console.error("ERR", err);
})
此API返回路由详细信息。 是否有一种方法可以让我在Google Node.JS客户端中获得与
isLocationOnPath
(Android SDK中的一种方法)功能相同的功能。

没有任何几何函数,但作为替代,您可以使用以下方法

googleMapsClient.directions({
    origin: 'Bangalore, Karnataka',
    destination: 'Mumbai, Maharathra',
    mode: "driving",
})
.asPromise()
.then((response) => {
    let points = []
    response.json.routes[0].legs[0].steps.forEach( (step) => {
        polyline.decode(step.polyline.points).forEach( (point) => {
            points.push([point[1], point[0]])
        })
    })
    let linestring = turf.lineString(points)
    let snapped = turf.nearestPointOnLine(linestring, turf.point([77.59464, 12.9726]))
    console.log(snapped.properties.dist)
})
.catch((err) => {
    console.log(err);
})

因此,如果snaped.properties.dist是snaped.properties.dist,恐怕唯一的方法就是获取源代码并在node.js中实现该函数(请参阅)