3

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.

  • You can leave the waypoints option out of the constructor, or set the waypoints: [] – Barbarossa Sep 9 '16 at 16:15
4

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.

1

Above solution of clearing doesn't work for me. However, I figured out more clean way to clear route with following command:

routeControl.getPlan().setWaypoints({latLng: L.latLng([0, 0])});

where routeControl is the instance of L.routing.control.

1

About the 2nd part _routes can't be accessed directly, but you can use the routesfound event right after .addTo(map) function:

routeControl.on('routesfound', function (e) {
  distance = e.routes[0].summary.totalDistance;
  time = e.routes[0].summary.totalTime;
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.