Closed LLStudent83 closed 1 year ago
LRM by default only fires a routesfound
event, when requesting the whole instruction set of the route.
When zooming, the router sets a geometryOnly
flag, which in turn leads to the routesfound
event being ignored.
If you wish to change this behaviour, the simplest way would be changing the _onZoomEnd
function
Good afternoon Alex. I still have a bad understanding of what is happening under the hood of the routing Machine. Based on the example from tutorial https://www.liedman.net/leaflet-routing-machine/tutorials/interaction / I wrote this:
const NewControl = L.Routing.Control.extend({
_onZoomEnd() {
if (!this._selectedRoute || !this._router.requiresMoreDetail) {
return;
}
const map = this._map;
if (this._router.requiresMoreDetail(this._selectedRoute, map.getZoom(), map.getBounds())) {
this.route({
callback: L.bind((err, routes) => {
let i;
if (!err) {
for (i = 0; i < routes.length; i++) {
this._routes[i].properties = routes[i].properties;
}
this._updateLineCallback(err, routes);
}
}, this),
simplifyGeometry: false,
geometryOnly: false,
});
}
},
});
onst createRoutineMachineLayer = (props, context) => {
const { constructionCoord, projectCoord, dataIconsForMap, color, routeId, setBuildingRoute, vehicle } = props;
const dispatch = useAppDispatch();
const plan = L.Routing.plan([L.latLng(constructionCoord), L.latLng(projectCoord)], {
createMarker: (i, start, n, dataIconsForMap) => createMarker(i, start, n, dataIconsForMap),
});
// const routingControl = L.Routing.control({
const routingControl = new NewControl({
plan,
router: new L.Routing.OSRMv1({
serviceUrl:
vehicle === 'foot'
? 'https://routing.openstreetmap.de/routed-foot/route/v1'
: 'https://router.project-osrm.org/route/v1',
}),
..........................................
}
Please tell me where I made a mistake?
This looks correct to me. Does it not work after zooming out? Would it be possible for you to create a codepen or codesandbox example so I could help in debugging? A very basic, stripped down version of your code that shows the problem is fine
Well I will try to do it soon.
Hello.After processing the "routingstart" event when the map zooms up, the code execution reaches line 310 where the callback is called. The code does not reach line 327. Therefore, it does not matter what value geometry Only has. And it may also be important, routing start is not called every time the map is zoomed. Maybe this will give you some thoughts? There is no time to create a code for research yet. Thank you so much for your time.
const NewControl = L.Routing.Control.extend({
_onZoomEnd() {
if (!this._selectedRoute || !this._router.requiresMoreDetail) {
return;
}
const map = this._map;
if (this._router.requiresMoreDetail(this._selectedRoute, map.getZoom(), map.getBounds())) {
this.route({
callback: L.bind((err, routes) => {
let i;
if (!err) {
for (i = 0; i < routes.length; i++) {
this._routes[i].properties = routes[i].properties;
}
this._updateLineCallback(err, routes);
}
this.fire('routesfound', { waypoints: routes[0].waypoints, routes });
}, this),
simplifyGeometry: false,
geometryOnly: false,
});
}
},
});
I wrote it like this. The problem is gone. But I'm not sure that this won't lead to some problems in the future.
Oh yeah, you are absolutely right! Sorry about that. The routesfound event is not used anywhere in LRM itself, so the code should work
Alex, forgive my curiosity. But I want to know why the "routingstart" event is triggered when zooming?
That's because zooming changes the required detail of the route, so it needs to be recalculated. The code responsible can be found here. Since this method is called on zoom and triggers another route request, as in your code above, it will also trigger a routingstart
event once the route request starts
Good morning Alex. Now it's clear. Thank you for your time. I hope our questions and answers will be useful to someone else
Good afternoon. I ask for help. There is a routing Machine (RM) component:
I listen to the "routingstart" and "routesfound" events to install and remove the loader. The problem is that after building the route with map = 8, RM sends a request to the server:
and receives the following response:
in this case, the "routesfound" event does not roll out and the application freezes with the loader. Please tell me what could be the problem?