How can I get the distance and time from L.Routing.control?
I am drawing a route on a leaflet map, it works good and in the control it shows the distance and estimated time of arrival. Is there a way to extract both of them and save them?
The code for the L.Routing.control
function getroute() {
myroutewithout = L.Routing.control({
waypoints: [
L.latLng(window.my_lat, window.my_lng),
L.latLng(window.job_p_lat, window.job_p_lng)
],show: true, units: 'imperial',
router: L.Routing.mapbox('API-KEY-HERE'),
createMarker: function(i, wp, nWps) {
if (i === 0 || i === nWps + 1) {
// here change the starting and ending icons
return mymarker = L.marker(wp.latLng, {
icon: operatoricon
});
} else {
return job_start = L.marker(wp.latLng, {
icon: jobicon
});
}
}
}).addTo(map);
javascript leaflet leaflet-routing-machine
add a comment |
I am drawing a route on a leaflet map, it works good and in the control it shows the distance and estimated time of arrival. Is there a way to extract both of them and save them?
The code for the L.Routing.control
function getroute() {
myroutewithout = L.Routing.control({
waypoints: [
L.latLng(window.my_lat, window.my_lng),
L.latLng(window.job_p_lat, window.job_p_lng)
],show: true, units: 'imperial',
router: L.Routing.mapbox('API-KEY-HERE'),
createMarker: function(i, wp, nWps) {
if (i === 0 || i === nWps + 1) {
// here change the starting and ending icons
return mymarker = L.marker(wp.latLng, {
icon: operatoricon
});
} else {
return job_start = L.marker(wp.latLng, {
icon: jobicon
});
}
}
}).addTo(map);
javascript leaflet leaflet-routing-machine
What do you mean "save" them - store in a variable to use elsewhere? What plugin are you using for routing?
– chazsolo
Dec 31 '18 at 15:14
Im using leaflet-routing-machine, and just for instance just alert them.
– larry chambers
Dec 31 '18 at 15:18
I don't see a straightforward way to expose that data, but the docs for the Router suggest that you can provide a callback that has an array of routes - each route contains an array of instructions, which include distance and time. You might be able to iterate over that?
– chazsolo
Dec 31 '18 at 15:30
@larrychambers Did you check my answer. Does it answer your question?
– kboul
Jan 7 at 9:49
add a comment |
I am drawing a route on a leaflet map, it works good and in the control it shows the distance and estimated time of arrival. Is there a way to extract both of them and save them?
The code for the L.Routing.control
function getroute() {
myroutewithout = L.Routing.control({
waypoints: [
L.latLng(window.my_lat, window.my_lng),
L.latLng(window.job_p_lat, window.job_p_lng)
],show: true, units: 'imperial',
router: L.Routing.mapbox('API-KEY-HERE'),
createMarker: function(i, wp, nWps) {
if (i === 0 || i === nWps + 1) {
// here change the starting and ending icons
return mymarker = L.marker(wp.latLng, {
icon: operatoricon
});
} else {
return job_start = L.marker(wp.latLng, {
icon: jobicon
});
}
}
}).addTo(map);
javascript leaflet leaflet-routing-machine
I am drawing a route on a leaflet map, it works good and in the control it shows the distance and estimated time of arrival. Is there a way to extract both of them and save them?
The code for the L.Routing.control
function getroute() {
myroutewithout = L.Routing.control({
waypoints: [
L.latLng(window.my_lat, window.my_lng),
L.latLng(window.job_p_lat, window.job_p_lng)
],show: true, units: 'imperial',
router: L.Routing.mapbox('API-KEY-HERE'),
createMarker: function(i, wp, nWps) {
if (i === 0 || i === nWps + 1) {
// here change the starting and ending icons
return mymarker = L.marker(wp.latLng, {
icon: operatoricon
});
} else {
return job_start = L.marker(wp.latLng, {
icon: jobicon
});
}
}
}).addTo(map);
javascript leaflet leaflet-routing-machine
javascript leaflet leaflet-routing-machine
edited Dec 31 '18 at 21:23
kboul
2,09021425
2,09021425
asked Dec 31 '18 at 15:10
larry chamberslarry chambers
1009
1009
What do you mean "save" them - store in a variable to use elsewhere? What plugin are you using for routing?
– chazsolo
Dec 31 '18 at 15:14
Im using leaflet-routing-machine, and just for instance just alert them.
– larry chambers
Dec 31 '18 at 15:18
I don't see a straightforward way to expose that data, but the docs for the Router suggest that you can provide a callback that has an array of routes - each route contains an array of instructions, which include distance and time. You might be able to iterate over that?
– chazsolo
Dec 31 '18 at 15:30
@larrychambers Did you check my answer. Does it answer your question?
– kboul
Jan 7 at 9:49
add a comment |
What do you mean "save" them - store in a variable to use elsewhere? What plugin are you using for routing?
– chazsolo
Dec 31 '18 at 15:14
Im using leaflet-routing-machine, and just for instance just alert them.
– larry chambers
Dec 31 '18 at 15:18
I don't see a straightforward way to expose that data, but the docs for the Router suggest that you can provide a callback that has an array of routes - each route contains an array of instructions, which include distance and time. You might be able to iterate over that?
– chazsolo
Dec 31 '18 at 15:30
@larrychambers Did you check my answer. Does it answer your question?
– kboul
Jan 7 at 9:49
What do you mean "save" them - store in a variable to use elsewhere? What plugin are you using for routing?
– chazsolo
Dec 31 '18 at 15:14
What do you mean "save" them - store in a variable to use elsewhere? What plugin are you using for routing?
– chazsolo
Dec 31 '18 at 15:14
Im using leaflet-routing-machine, and just for instance just alert them.
– larry chambers
Dec 31 '18 at 15:18
Im using leaflet-routing-machine, and just for instance just alert them.
– larry chambers
Dec 31 '18 at 15:18
I don't see a straightforward way to expose that data, but the docs for the Router suggest that you can provide a callback that has an array of routes - each route contains an array of instructions, which include distance and time. You might be able to iterate over that?
– chazsolo
Dec 31 '18 at 15:30
I don't see a straightforward way to expose that data, but the docs for the Router suggest that you can provide a callback that has an array of routes - each route contains an array of instructions, which include distance and time. You might be able to iterate over that?
– chazsolo
Dec 31 '18 at 15:30
@larrychambers Did you check my answer. Does it answer your question?
– kboul
Jan 7 at 9:49
@larrychambers Did you check my answer. Does it answer your question?
– kboul
Jan 7 at 9:49
add a comment |
1 Answer
1
active
oldest
votes
You can achieve that using the code from this issue
var routeControl = L.Routing.control({...});
...
routeControl.on('routesfound', function(e) {
var routes = e.routes;
var summary = routes[0].summary;
// alert distance and time in km and minutes
alert('Total distance is ' + summary.totalDistance / 1000 + ' km and total time is ' + Math.round(summary.totalTime % 3600 / 60) + ' minutes');
});
Demo
1
Sorry not yet, I will do in hour or so thanks
– larry chambers
Jan 7 at 10:16
1
Yes works great apart from it only shows time in minutes and misses out the hours! But I can work with that, thankyou
– larry chambers
Jan 7 at 12:54
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53988872%2fhow-can-i-get-the-distance-and-time-from-l-routing-control%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can achieve that using the code from this issue
var routeControl = L.Routing.control({...});
...
routeControl.on('routesfound', function(e) {
var routes = e.routes;
var summary = routes[0].summary;
// alert distance and time in km and minutes
alert('Total distance is ' + summary.totalDistance / 1000 + ' km and total time is ' + Math.round(summary.totalTime % 3600 / 60) + ' minutes');
});
Demo
1
Sorry not yet, I will do in hour or so thanks
– larry chambers
Jan 7 at 10:16
1
Yes works great apart from it only shows time in minutes and misses out the hours! But I can work with that, thankyou
– larry chambers
Jan 7 at 12:54
add a comment |
You can achieve that using the code from this issue
var routeControl = L.Routing.control({...});
...
routeControl.on('routesfound', function(e) {
var routes = e.routes;
var summary = routes[0].summary;
// alert distance and time in km and minutes
alert('Total distance is ' + summary.totalDistance / 1000 + ' km and total time is ' + Math.round(summary.totalTime % 3600 / 60) + ' minutes');
});
Demo
1
Sorry not yet, I will do in hour or so thanks
– larry chambers
Jan 7 at 10:16
1
Yes works great apart from it only shows time in minutes and misses out the hours! But I can work with that, thankyou
– larry chambers
Jan 7 at 12:54
add a comment |
You can achieve that using the code from this issue
var routeControl = L.Routing.control({...});
...
routeControl.on('routesfound', function(e) {
var routes = e.routes;
var summary = routes[0].summary;
// alert distance and time in km and minutes
alert('Total distance is ' + summary.totalDistance / 1000 + ' km and total time is ' + Math.round(summary.totalTime % 3600 / 60) + ' minutes');
});
Demo
You can achieve that using the code from this issue
var routeControl = L.Routing.control({...});
...
routeControl.on('routesfound', function(e) {
var routes = e.routes;
var summary = routes[0].summary;
// alert distance and time in km and minutes
alert('Total distance is ' + summary.totalDistance / 1000 + ' km and total time is ' + Math.round(summary.totalTime % 3600 / 60) + ' minutes');
});
Demo
edited Jan 1 at 17:43
answered Dec 31 '18 at 18:14
kboulkboul
2,09021425
2,09021425
1
Sorry not yet, I will do in hour or so thanks
– larry chambers
Jan 7 at 10:16
1
Yes works great apart from it only shows time in minutes and misses out the hours! But I can work with that, thankyou
– larry chambers
Jan 7 at 12:54
add a comment |
1
Sorry not yet, I will do in hour or so thanks
– larry chambers
Jan 7 at 10:16
1
Yes works great apart from it only shows time in minutes and misses out the hours! But I can work with that, thankyou
– larry chambers
Jan 7 at 12:54
1
1
Sorry not yet, I will do in hour or so thanks
– larry chambers
Jan 7 at 10:16
Sorry not yet, I will do in hour or so thanks
– larry chambers
Jan 7 at 10:16
1
1
Yes works great apart from it only shows time in minutes and misses out the hours! But I can work with that, thankyou
– larry chambers
Jan 7 at 12:54
Yes works great apart from it only shows time in minutes and misses out the hours! But I can work with that, thankyou
– larry chambers
Jan 7 at 12:54
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53988872%2fhow-can-i-get-the-distance-and-time-from-l-routing-control%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What do you mean "save" them - store in a variable to use elsewhere? What plugin are you using for routing?
– chazsolo
Dec 31 '18 at 15:14
Im using leaflet-routing-machine, and just for instance just alert them.
– larry chambers
Dec 31 '18 at 15:18
I don't see a straightforward way to expose that data, but the docs for the Router suggest that you can provide a callback that has an array of routes - each route contains an array of instructions, which include distance and time. You might be able to iterate over that?
– chazsolo
Dec 31 '18 at 15:30
@larrychambers Did you check my answer. Does it answer your question?
– kboul
Jan 7 at 9:49