perliedman / leaflet-routing-machine

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

Get route time for each waypoint to another #569

Closed LefterisMavrakis closed 4 years ago

LefterisMavrakis commented 4 years ago

Hi there, Is there any way to get the time between each route? I have a project that i implementing in vue.js and the concept is that i have orders to be delivered represented by checkboxes and when i click them i add the waypoint for each order to the map using the setWaypoints method. When i click again im deleting the waypoint with the spliceWaypoint method. Whenever im adding a new waypoint i always pushing the first waypoint at last position of waypoint array in order to have a return route. Till now i only have the summary of total time for all routes. So if we say that i have 4 waypoints i want to calculate the time between the 3rd and 4th waypoint. Is it possible?

perliedman commented 4 years ago

Each route has an array of instructions in it, which also contains the time (in seconds) for that part of the route. There is a special instruction when a waypoint is reached. You can sum instruction times between waypoint to get the time per route.

LefterisMavrakis commented 4 years ago

Hello Per Thank you for your fast answer. Problem solved!

Another solution with vue.js is to create a watcher for the routing time summary which has to be a data property that feeling with the values of time summary from the routing control. So when you create the watcher for this data property you can subtract the new value from the old and the result will be the time for the clicked route. Then you can assign the result to your final orders object. I think your solution is better because i can compare many values from the routesfound event. So thank you again. Keep up with the nice work!!

LefterisMavrakis commented 4 years ago

Hello Per

Can you please tell me which property or option from the routing control does the routesfound event reads in order to find routes ?

Ill explain my problem:

I have orders to be delivered represented by checkboxes and when i click them i add the waypoint for each order to the map using the setWaypoints method. When i click again im deleting the waypoint with the spliceWaypoint method. When i click a checked checkbox and splice it from routing control waypoints at the same time routesfound event doesnt know that the waypoint is spliced and when i click again the checkbox to set the waypoint again, the routesfound event fires two times. If i uncheck the checkbox and then check it again, the routesfound event fires 3 times and so on, where i have only one waypoint added to the routing control. I believe that i have to update the property that the routesfound event reads whenever i splice a waypoint or find a way to clear the routes from routesfound event whenever i splice a waypoint. Am i right?

LefterisMavrakis commented 4 years ago

I have already try to update some properties of the routing control object: this.routingControl._routes[0].waypoints.splice(index,1); this.routingControl._plan._waypoints.splice(index,1); But no luck !

perliedman commented 4 years ago

Hi, not entirely clear to me what is happening here, hard to tell without seeing the code running live.

That said, you should never have to update anything prefixed with underscore (_), those variables are "private" in the sense that they are not intended to be changed by the code using the library, and not part of the API.

The routesfound event does not "read from a property" if I understand the question correctly, it fires directly after a valid route response is returned from the server. You can see the logic in question here: https://github.com/perliedman/leaflet-routing-machine/blob/master/src/control.js#L328