[GIS] leaflet-routing-machine add waypoints, routing summary

leafletrouting

I'm playing around with leaflet-routing-machine (https://github.com/perliedman/leaflet-routing-machine) and have 2 questions about it's usage:

1)

I have the following code now

routeControl = L.Routing.control({
     waypoints: [
                 L.latLng(48.133205,11.565225),
                 L.latLng(48.131830,11.588227)
             ],
    useZoomParameter: false,
    autoRoute: false,
}).addTo(map);

which works fine, but what when i want to start without any waypoints? When i leave out the waypoints from the constructor and try to add it then with

routeControl.spliceWaypoints(0, 0, e.latlng);

routing is not working anylonger (i know, i need to start routing manually, because I set autoRoute to false). But if I start with those initial points and add further waypoints, routing is working.

2) I'm trying to get totalDistance, which is part of the Interface IRouteSummary.

According to the API, I need to access the IRoute Interface, but i didn't find out how to get IRoute from L.Routing.Control

I found this post (https://stackoverflow.com/questions/26570980/getting-distance-between-2-markers) which uses an array of routes[], but i also don't know how to get this array?! It also refers to this Tutorial http://www.liedman.net/leaflet-routing-machine/tutorials/interaction/ but here i don't find any information about how to access this array as well.

Best Answer

I can answer the 2nd part of your question, the first one has me a little tied up now too I'll edit if no one answers when I get my head around it. So for 2) just use something in the line of

routeControl._routes[0].summary.totalDistance

See if this works, also _routes[0] represents your choice for example if you have another routing option for which you want to find out the distance _routes[1] is your answer...integrate this into a function and there it is.

Edit: Regarding your first problem starting without a route just call the following:

L.routing.control.({
    waypoints: [null
    ]
  }) 

And assuming you want to add new waypoints

L.routing.control.setWaypoints([
    L.latLng(lat1, lon1),
    L.latLng(lat2, lon2)
  ]);

This syntax is very weird indeed but pop open a code inspector and you'll get the hang of it.