I am using LRM to calculate routes. And I need data about this routes. Now I'm just learning how to exctract this data so I can use it further.
I have two routes
var routing1 = L.Routing.control({
waypoints: [
L.latLng(54.736038, 55.97429),
L.latLng(54.736985, 55.980878),
],
});
var routing2 = L.Routing.control({
waypoints: [
L.latLng(54.732798, 55.969934),
L.latLng(54.734954, 55.95809)
],
});
I have a button to draw this routes
document.getElementById("drawing").addEventListener("click", myFunction);
function myFunction() {
routing1.addTo(map).on('routesfound', function (e) {
distance = e.routes[0].summary.totalDistance;
console.log('routing1 ' + distance);
});
routing2.addTo(map).on('routesfound', function (e) {
distance = e.routes[0].summary.totalDistance;
console.log('routing2 ' + distance);
});
}
As I don't quite understand JS yet so I have this question:
How can I console.log route's summary the moment route calculated? Without drawing it.
Best Answer
EDIT1: Solution below was first attempt. It is working, but is quite complicates. See simple solution at EDIT2.
Upon investigation it turns out that quite a big part of routing process depends on adding
L.Routing.control
to map. To force route calculation and prevent showing of route on the map requires some hacking trickery:show: false
option.createMarker: function(p1,p2) {}
._updateLines
method has to be replaced with dummy method.onAdd(map)
method.Final code then looks something like this:
EDIT2: Much simpler solution uses
L.Routing.osrmv1
routing method: