perliedman / leaflet-routing-machine

Control for routing in Leaflet
https://www.liedman.net/leaflet-routing-machine/
Other
1.06k stars 347 forks source link

Delete/Remove only the route and not route points using spliceWayPoints. #639

Open himanshug16 opened 2 years ago

himanshug16 commented 2 years ago

Hi, Thanks for the great library.

I am using leaflet for maps and using leaflet routing machine to create routes between the locations.

But unable to find a way to manipulate the waypoints. so that i can hide routes between 2 points. If there are only 2 locations then it works perfectly fine as it deletes the complete connection. but if we are creating 3 routes points then 2nd point will act as a middle point between 1 and 3 and with splice way point function, it clears the complete points instead of just the route.

routingControl.getPlan().setWaypoints([]); using this to set route points to leaflet map.

is there a way/function by which we can manipulate addition/removal of just the routes between the 2 points and not the point/location itself?

Snapshot of function to remove the point is attached.

Any help from the community is much appreciated!

Screenshot 2021-08-30 at 7 20 00 PM
curtisy1 commented 2 years ago

Hey @himanshug16 If I understood the question correctly this is not possible using LRM itself because the actual routes are returned by whatever routing endpoint you use (e.g. OSRM, Mapbox, etc.). This means that if you have a total of three waypoints, the route is rendered as the endpoint returns it.

One way to "intercept" what results you get is by extending your routing provider and customizing the route function. For OSRMv1 this could look something like this:

const customRouter = OSRMv1.extend({
        options: {
            serviceUrl: 'https://api.mapbox.com/directions/v5',
            profile: 'mapbox/driving',
            useHints: false
        },

        route: function(waypoints, callback, context, options) {
            const routeResult = L.Routing.OSRMv1.prototype.route.call(this, waypoints, _customCallback, context, options);

        }

        _customCallback: function(...) {
            // any custom logic here, i.e. removing the first 5 waypoints
            const modifiedRoute = routeResult.splice(0, 5);
        }
    });

You could probably also make use of the routesfound event that gets triggered after routing is done and interact with the returned routes directly.