Search code examples
javascriptleafletleaflet-routing-machine

leaflet routing machine - IRoute.summary.totalDistance


I am using Leaflet 1.2.0 and Leaflet Routing Machine 3.2.12.

My code is drawing the correct route on the map, yet, when i want to access the summary and the totalDistance from the summary, both are undefined. Why is that?

function createRoute(id, coords) {
            route = L.Routing.control({
                    name: id,
                    serviceUrl: 'http://router.project-osrm.org/route/v1',
                    //router: L.Routing.graphHopper('apiKey');
                    waypoints: coords, //way_points,
                    addWaypoints: false,
                    draggableWaypoints: false,
                    show: false,
                }).on('routesfound', function(e) {
                    console.log(this.summary.totalDistance);
                    // console.log(route.summary.totalDistance);
                     
                })
                    //.on('routesfound', function(e) {
                    //    routes = e.routes;  //is declared and instantiated before the function 
                    //                        // createRoute
                    //    RouteLength = routes[0].summary.totalDistance;
                    //});
                .addTo(map);
    }

The error: Routing Error: status -3 TypeError: Cannot read properties of undefined (reading 'totalDistance').

From the console i get the escalation of:

fire    @   leaflet.js:5
(anonym)    @   leaflet-routing-machine.js:16166
(anonym)    @   leaflet-routing-machine.js:18004
loaded  @   leaflet-routing-machine.js:46
load (asynchron)        
corslite    @   leaflet-routing-machine.js:53
route   @   leaflet-routing-machine.js:17977
route   @   leaflet-routing-machine.js:16151
onAdd   @   leaflet-routing-machine.js:15919
addTo   @   leaflet.js:5
createRoute @   POI-Dash implement Routing.html:586

Anyone know more about it?


Solution

  • this doesn't have a summary. You will need to refer to e.routes, like e.routes[0].summary.totalDistance.