Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check existence of point on leaflet route

Is there are a method on leaflet routing machine that checks if a specific point, e.g (lat,lng), is inside the routing polyline ?

like image 484
Bassem Salama Avatar asked Oct 20 '25 06:10

Bassem Salama


1 Answers

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;
}
like image 61
Anatoly Sukhanov Avatar answered Oct 21 '25 21:10

Anatoly Sukhanov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!