I used to be able to get it like this:
directionsService.route(directionsRequest, function(directionsResult, directionsStatus) {
var directionsRenderer = new google.maps.DirectionsRenderer({
directions: directionsResult,
map: map
});
$('#distance').text(directionsResult.trips[0].routes[0].distance.text)
$('#duration').text(directionsResult.trips[0].routes[0].duration.text)
})
But it looks like they changed their API on me! Looks like trips
is no longer there, and routes
only gives you a bunch of legs... do I really have to iterate over all the legs and sum up the distance now?
As per Leniel's answer:
Actually, this works just fine too, if you don't have any waypoints:
Here's a fuller example using lodash. Shouldn't be too hard to replace
flatBy
andsum
if you're not using it.Remember to include the Google Maps API:
Also, I'm pretty sure their ToS require you to display a Google Map too.