Is there are a method on leaflet routing machine that checks if a specific point, e.g (lat,lng), is inside the routing polyline ?
There is not, but it's still doable using Leaflet.GeometryUtil
Take a look at the belongsSegment function:
belongsSegment(latlng, latlngA, latlngB, toleranceopt, nullable) → {boolean}
Returns true if the latlng belongs to segment A-B
So when a route is selected in Routing Machine, you are checking for every segment of a polyline if the point belongs to it with a given tolerance:
var point = {your specific point};
...
map.on('routeselected', function(e) {
var route = e.route;
isPointOnLine(point, route.coordinates));
})
where isPointOnLine is
function isPointOnLine(point, path) {
for (var i = 0; i < path.length - 1; i++) {
if (L.GeometryUtil.belongsSegment(point, path[i], path[i + 1])) {
return true;
}
}
return false;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With