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 8546937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:09:01+00:00 2026-06-11T13:09:01+00:00

I’m using the Google Maps API to get driving distances from multiple routes. Each

  • 0

I’m using the Google Maps API to get driving distances from multiple routes. Each route is being taken by a different driver; they all have the same start point but different endpoints and might have multiple waypoints.

The problem I’m having is that the API doesn’t seem to give me any way to name each route, so when they come back asynchronously, I can’t be sure which route belongs to which driver. It’s true that each route has different addresses, but the DirectionsService response object gives me back slightly different addresses than I send — “Des Plaines, IL” becomes “Des Plaines, IL, USA”, for instance — making it unreliable to compare those.

The API doesn’t seem to let me send arbitrary strings with the request object to name them uniquely. So what can I do?

A code snippet that might help clarify what I’m doing:

$.getJSON(ajaxsource, function(data) {
    var darr = data.driver_info, // includes driver name, start and end addresses
        driver, start, end,
        waypts = [];
    for (var i=0,j=darr.length; i<j; i++) {
        if (driver==$.trim(darr[i][8])) { // same driver, next destination
            //  start = end;
            waypts.push({
                location: end,
                stopover: true });
        } else {
            waypts = [];
            driver = $.trim(darr[i][8]);
            start = $.trim(darr[i][4]);
        }
        end = $.trim(darr[i][2]);

        if (i+1==j || driver!=$.trim(darr[i+1][8])) { 
            var request = { // route object
                origin: start,
                destination: end,
                waypoints: waypts,
                optimizeWaypoints: true,
                travelMode: google.maps.DirectionsTravelMode.DRIVING
            };
            directionsDisplay = new google.maps.DirectionsRenderer();
            directionsService.route(request, function(response, status) {
                var legs = response.routes[0].legs;
                // console.log(response);
                for (var k=0; k<legs.length; k++) {
                    $('#results').append($('<li>').html("Driver " + driver + 
                        "<br>starts at " + legs[k].start_address + 
                        "<br>and drives " + legs[k].distance.text + 
                        "<br>to end at " + legs[k].end_address));
                }
            });
        };

    }; // end for
}); // end getJSON

That code will return all the route info as desired, but since it’s asynchronous, the driver in each result is the name of the last driver listed in my data.driver_info array. What I need is to list the driver that should be associated with that particular route.

  • 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-06-11T13:09:02+00:00Added an answer on June 11, 2026 at 1:09 pm

    A little more debugging uncovered a solution — the Google documentation didn’t mention that the response object doesn’t just contain a status and routes, but also a sub-object called ub which mirrors the request object I sent. Using that, I can compare addresses exactly to match routes to drivers.

    Object
        routes: Array[1]
        status: "OK"
        ub: Object
            destination: "1234 Some Street, Antioch, IL"
            optimizeWaypoints: true
            origin: "2345 Anywhere Ave., Lake Forest, IL"
            travelMode: "DRIVING"
            waypoints: Array[0]
            __proto__: Object
        __proto__: Object
    

    I added the following code inside my directionsService.route() callback:

    var driver = '',
        end_ub = response.ub.destination;
    for (var a=0,b=darr.length; a<b; a++) {
        if (darr[a][2]==end_ub) {
            driver = darr[a][8];
            break;
        };
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a simple page using Google Maps API 3. My first. One marker
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.