Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 919081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:28:59+00:00 2026-05-15T18:28:59+00:00

I used to be able to get it like this: directionsService.route(directionsRequest, function(directionsResult, directionsStatus) {

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T18:29:00+00:00Added an answer on May 15, 2026 at 6:29 pm

    As per Leniel’s answer:

    var totalDistance = 0;
    var totalDuration = 0;
    var legs = directionsResult.routes[0].legs;
    for(var i=0; i<legs.length; ++i) {
        totalDistance += legs[i].distance.value;
        totalDuration += legs[i].duration.value;
    }
    $('#distance').text(totalDistance);
    $('#duration').text(totalDuration);
    

    Actually, this works just fine too, if you don’t have any waypoints:

    $('#distance').text(directionsResult.routes[0].legs[0].distance.text);
    $('#duration').text(directionsResult.routes[0].legs[0].duration.text);
    

    Here’s a fuller example using lodash. Shouldn’t be too hard to replace flatBy and sum if you’re not using it.

    /**
     * Computes the total driving distance between addresses. Result in meters.
     *
     * @param {string[]} addresses Array of address strings. Requires two or more.
     * @returns {Promise} Driving distance in meters
     */
    export default function calculateDistance(addresses) {
        return new Promise((resolve, reject) => {
            if(addresses.length < 2) {
                return reject(new Error(`Distance calculation requires at least 2 stops, got ${addresses.length}`));
            }
    
            const {TravelMode, DirectionsService, DirectionsStatus} = google.maps;
    
            const directionsService = new DirectionsService;
            const origin = addresses.shift();
            const destination = addresses.pop();
            const waypoints = addresses.map(stop => ({location: stop}));
    
            directionsService.route({
                origin,
                waypoints,
                destination,
                travelMode: TravelMode.DRIVING,
            }, (response, status) => {
                if(status === DirectionsStatus.OK) {
                    let distances = _.flatMap(response.routes, route => _.flatMap(route.legs, leg => leg.distance.value));
    
                    return resolve(_.sum(distances));
                } else {
                    return reject(new Error(status));
                }
            });
        });
    }
    

    Remember to include the Google Maps API:

    <script src="https://maps.googleapis.com/maps/api/js?key=API_KEY&libraries=places"></script>
    

    Also, I’m pretty sure their ToS require you to display a Google Map too.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I used to be able to do something like this to get a nested
used to be able to do this without any problems: for (data in dataArray)
I used to be able to open a new buffer in Emacs quickly using
I used to be able to do this to create an exported HTML page
Lets say I have a class like this: Class User { var $id var
In mixing Linq-to-SQL and Linq-to-XML, I used to be able to do something like
I'd like to be able to get the bits from a System.Decimal value and
This might sound like a stupid question, I am new to C++. On debian
I need to be able to hande routes like this: appdomain/city/City-state, so in case
I'm trying to convert mscorlib.tlb. It normally used in C++ like this: #import mscorlib.tlb

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.