-
Notifications
You must be signed in to change notification settings - Fork 353
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Leaflet Routing Machine: How to edit a route before rendering #664
Comments
If you want the markers only for the waypoints you set yourself, then the plan takes a
The coordinates are all the points the routing engine found. So let's say you want to go from A to Z, then the routing engine might suggest you to travel via coordinates B, C, etc. What is returned there is entirely up to the routing engine and plugin implementation. The OSRM plugin for example, does it this way. An example of how extending could work (if you're not using typescript) can be found in the mapbox implementation |
Hi there, Thanks for replying! I appreciate it! Sorry for the tardy reply!
I figured I do a conditional in the plan to see all the Markers which are not the first or last, and then what I would do is push in those coordinates which make the route. But now that I think about it, won't this create markers using the plan prop? What I would like is to change the lat and lng of the points in between the starting and ending point thereby augmenting the route. This is my repo if you'd like to see it all the code! |
You're absolutely right, not sure how I got to the createMarker function. Sorry about that!
Then I believe your only option would be a custom router implementation. In general it could look something like this const customCallback = (callback) => (context, error, routes) => {
for (const route of routes) {
route.coordinates = [...route.coordinates, L.latLng(12.33, 49.6753)];
}
callback(context, error, routes);
};
const customRouter = OSRMv1.extend({
initialize: function(options) {
L.Routing.OSRMv1.prototype.initialize.call(this, options);
},
route: function(waypoints, callback, context, options) {
const originalCallback = options.callback;
L.Routing.OSRMv1.prototype.route.call(waypoints, customCallback(originalCallback), this, options);
}
}); |
Hey thanks for posting this my friend. I am going to give a whirl now! |
Hi there again. I tried to integrate your example like so:
But I am getting :
I am trying to debug it but wondering if there is documentation for adding this? |
You're on the right track. Unfortunately, there isn't any great example other than the existing implementations. So instead of router: L.Routing.OSRMv1.extend({
initialize: function (options) {
L.Routing.OSRMv1.prototype.initialize.call(this, options);
},
route: function (waypoints, callback, context, options) {
const originalCallback = options.callback;
L.Routing.OSRMv1.prototype.route.call(waypoints, customCallback(originalCallback), this, options);
}
}), you'll have to do const router = L.Routing.OSRMv1.extend({
initialize: function (options) {
L.Routing.OSRMv1.prototype.initialize.call(this, options);
},
route: function (waypoints, callback, context, options) {
const originalCallback = options.callback;
L.Routing.OSRMv1.prototype.route.call(waypoints, customCallback(originalCallback), this, options);
}
});
const routingControl = L.Routing.control({
addWaypoints: false,
collapsible: true,
draggableWaypoints: true,
lineOptions: {
styles: [{ color: 'chartreuse', opacity: 1, weight: 5 }]
},
position: 'bottomright',
router: new router(),
routeWhileDragging: true,
show: true,
showAlternatives: false,
waypoints
}).addTo(map) |
Hi there thanks for the information! And the help with this. So it is fair to say, this is just adding props to a function to override the So when you call Like if you console logged
However using what you recommended I am getting this error.
Also and finally I thought you would be doing something like this with the
And then in the Routing control using it like this:
This is the repo if you need it... Again thanks for the help! |
You could say so. In React terms, extending would probably be like writing a higher order component (HOC).
My bad, I forgot Leaflet requires context as the first argument (unless you bind it to a specific instance?). L.Routing.OSRMv1.prototype.route.call(
this, // this one is important. See https://leafletjs.com/examples/extending/extending-1-classes.html
waypoints,
customCallback(originalCallback),
this,
options
); That should work. Also running your project, I noticed you'll have to swap the arguments in the custom callback const customCallback = (callback) => (context, routes, error) => {
if(!routes) return
for (const route of routes) {
route.coordinates = [...route.coordinates, L.latLng(12.33, 49.6753)];
}
callback(context, error, routes);
};
|
While writing this, I realized the whole extend thing isn't really necessary anymore since JS supports native classes now. const customCallback = (callback) => (context, routes, error) => {
if (!routes) {
return;
}
for (const route of routes) {
route.coordinates = [...route.coordinates, L.latLng(12.33, 49.6753)];
}
callback(context, error, routes);
};
class CustomRouter extends L.Routing.OSRMv1 {
constructor(options) {
super(options); // super is L.Routing.OSRMv1
}
route(waypoints, callback, context, options) {
console.log("waypoints ", waypoints);
console.log("callback ", callback);
console.log("context", context);
console.log("options ", options);
const originalCallback = options.callback;
super.route(
waypoints,
customCallback(originalCallback),
this,
options
);
}
} |
I've created a circle on the map and in that circle I've figured a point with the highest elevation and one with the lowest. (I used this leaflet-plugin to get me that data)
Those two point are my starting and ending points in Leaflet-Routing Machine.
In the Leaflet Routing Machine documentation there is a sub-object called an interface.
When clicking one of them called IRoute it takes you to:
There is a property called coordinates
So what I would like is to add some custom points in the route, The end result would be each point from the starting point would be lower in elevation from the last until the end point.
I did a console of my L.Routing.Control instance and see this:
I see the coordinates prop but not sure if that's even the right prop?
So essentially I want to add custom markers/latlang to the route generated.
Thanks in advance!
The text was updated successfully, but these errors were encountered: