[GIS] How to get the total km of route using Leaflet Routing Machine API

leaflet

I'm using machine leaflet route to draw routes I can get the total route miles? How can I do this?

I know the class IRouteSummary, but I am unable to access it.
Just wanted an example so that I can access the returns of classes leaflet machine route
This is my code:

L.Routing.control ({
waypoints: [
L.latLng (-30.050390, -51.198338) 
L.latLng (-30.024472, -51.219392) 
] 

}) addTo (map).; 


var router = L.Routing.osrm () 
     waypoints = [], 
     line; 

map.on ('click', function (e) {
     waypoints.push ({latLng: e.latlng}); 
     if (waypoints.length> = 2) {
         router.route (waypoints, function (err, routes) {
             if (line) {
                 map.removeLayer (line); 
             } 

             if (err) {
                 alert (err); 
             } Else {
                 line = L.Routing.line (routes [0]) addTo (map).; 
             } 
         }); 
     } 
});

Best Answer

You are already accessing the IRoute object (by routes[0]), so the rest is about getting in touch with IRouteSummary object from which you can get the distance:

alert('Distance: ' + routes[0].summary.totalDistance);