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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T11:55:20+00:00 2026-06-16T11:55:20+00:00

I have been playing a little bit with Google’s DistanceMatrixService. The code below works,

  • 0

I have been playing a little bit with Google’s DistanceMatrixService. The code below works, but, how can I pass another parameter to the callback function or grab one of the values out of the callback?

For example: I have two divs that I want to show different results in (Results1 and Results2), so I am thinking I need to either
pass another value to the GoogleMapDistance function like GoogleMapDistance(YourLatLong,DestLatLong,TheDiv)
or
be able to grab the ResultStr externally outside of the callback document.getElementById(“Results1”).innerHTML = ResultStr;
or
set the innerHTM to the returned value of the function document.getElementById(“Results1”).innerHTML = GoogleMapDistance(YourLatLong,DestLatLong);

I’m stuck. How can I do accomplish this? The way it looks right now is that I am only going to be able to run all this code once and have it only write to one div.

<div id="Results1"></div>
<div id="Results2"></div>

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script>

function GoogleMapDistance(YourLatLong,DestLatLong)
{
    var service = new google.maps.DistanceMatrixService();
    service.getDistanceMatrix(
    {
    origins: [YourLatLong],
    destinations: [DestLatLong],
    travelMode: google.maps.TravelMode.DRIVING,
    unitSystem: google.maps.UnitSystem.IMPERIAL,
    avoidHighways: false,
    avoidTolls: false
    }, callback);
}

function callback(response, status)
{
    if (status == google.maps.DistanceMatrixStatus.OK)
    {
    var origins = response.originAddresses;
    var destinations = response.destinationAddresses;
      for (var i = 0; i < origins.length; i++)
      {
          var results = response.rows[i].elements;
          for (var j = 0; j < results.length; j++)
          {
              var element = results[j];
              var from = origins[i];
              var to = destinations[j];
              var distance = element.distance.text;
              var duration = element.duration.text;
              var ResultStr = distance + "&nbsp; (<i>" + duration + "</i>)";
          }
      }
    document.getElementById("Results1").innerHTML = ResultStr;
    }
}

var YourLatLong = "45.4049,-122.797997";
var DestLatLong1 = "47.468893,-122.227978";
var DestLatLong2 = "61.221274,-149.831545";

GoogleMapDistance(YourLatLong,DestLatLong1);

</script>
  • 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-16T11:55:21+00:00Added an answer on June 16, 2026 at 11:55 am

    You can’t change how Google calls the callback, but you can let it call your own locally function as the callback and then have that (via a closure) call another callback after adding the desired extra argument like this:

    function GoogleMapDistance(YourLatLong,DestLatLong, item)
    {
        var service = new google.maps.DistanceMatrixService();
        service.getDistanceMatrix(
        {
        origins: [YourLatLong],
        destinations: [DestLatLong],
        travelMode: google.maps.TravelMode.DRIVING,
        unitSystem: google.maps.UnitSystem.IMPERIAL,
        avoidHighways: false,
        avoidTolls: false
        }, function(response, status) {callback(response, status, item)});
    }
    

    Or, you could just define your callback inline so it has access to the parent function variables directly:

    function GoogleMapDistance(YourLatLong,DestLatLong, item)
    {
        var service = new google.maps.DistanceMatrixService();
        service.getDistanceMatrix(
        {
        origins: [YourLatLong],
        destinations: [DestLatLong],
        travelMode: google.maps.TravelMode.DRIVING,
        unitSystem: google.maps.UnitSystem.IMPERIAL,
        avoidHighways: false,
        avoidTolls: false
        }, function callback(response, status)
        {
            // you can access the parent scope arguments like item here
            if (status == google.maps.DistanceMatrixStatus.OK)
            {
            var origins = response.originAddresses;
            var destinations = response.destinationAddresses;
              for (var i = 0; i < origins.length; i++)
              {
                  var results = response.rows[i].elements;
                  for (var j = 0; j < results.length; j++)
                  {
                      var element = results[j];
                      var from = origins[i];
                      var to = destinations[j];
                      var distance = element.distance.text;
                      var duration = element.duration.text;
                      var ResultStr = distance + "&nbsp; (<i>" + duration + "</i>)";
                  }
              }
            document.getElementById("Results1").innerHTML = ResultStr;
            }
        }
    
    )}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been playing around with Direct3D 11 a little bit lately and have been
I have been playing around with SQLite a little bit and I was looking
Earlier it was working fine. I have been playing little bit config. So may
I have been playing with Expect/TCL today and I was hoping someone can tell
I have been playing around with Google Apps Script today and I am trying
I have been playing around a bit with a fairly simple, home-made search engine,
I did a little GP (note:very little) work in college and have been playing
I've been playing around a little with the Exocortex implementation of the FFT, but
I've been playing around with floating point numbers a little bit, and based on
I have been playing around with Dynamic Data but I find the filtering and

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.