0
votes

I'm actually working for a school project with the Leaflet Routing Machine and having issue withe the totalTime method.

    var map = L.map('map');

L.tileLayer('', {
    attribution: ''
}).addTo(map);

L.Routing.control({
    waypoints: [
        L.latLng(45.7979, 4.7178),
        L.latLng(45.5221, 4.8101),
        L.latLng(45.42, 4.35)
    ],
    routeWhileDragging: true,
    language: 'fr'
}).addTo(map);

L.Routing.control({
    waypoints: [
        L.latLng(44.7979, 4.7178),
        L.latLng(45.42, 4.35)
    ],
    routeWhileDragging: true,
    language: 'fr'
}).addTo(map);

window.alert(routes[0].summary.totalTime());

But JS console is telling me that routes[] isn't defined.

Is there anything wrong ? Should we use something else than routes[] ?

1
You, uh, haven't defined routes anywhere in this code. - GAntoine
Sure, but i would like too call the method totalTime. liedman.net/leaflet-routing-machine/api/#irouter I've found this topic : stackoverflow.com/questions/26570980/… So i tried it. - koolok

1 Answers

0
votes

Your problem is related to the fact that the 'routesfound' event is not used on the Routing control. Try to add the event and handle the distance and time results inside the handler

routeCtr.addTo(map).on('routesfound', function (e) {
        distance = e.routes[0].summary.totalDistance;
        min = e.routes[0].summary.totalTime;
};

Try it.