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 = [], 

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).; 

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);
| improve this answer | |

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.