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

  • Home
  • SEARCH
  • 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 504203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:28:21+00:00 2026-05-13T06:28:21+00:00

I am simply trying to get the distance from 2 points on a map

  • 0

I am simply trying to get the distance from 2 points on a map using the Google Maps API. Using GDirections. The problem is that after the function finishes, distance is always null. I know this is because the “load” event isn’t being called until after the function finishes. The Event Listener doesn’t return values either so I am stumped!

Does anybody know how I can make this function return the distance? Perhaps there is a better way to get the distance between 2 points in Google Maps API?

function getDistance(fromAddr, toAddr) {    
var distance;
var directions;

directions = new GDirections(null, null);
directions.load("from: " + fromAddr + " to: " + toAddr);

GEvent.addListener(directions, "load", function() {
    distance = directions.getDistance().html;
    distance = distance.replace(/&.*/, '');
});

return distance; //outputs null
}
  • 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-13T06:28:22+00:00Added an answer on May 13, 2026 at 6:28 am

    GDirections load is asynchronous. You won’t be able t use the results of your load request until the load event is fired. This means that your getDistance function just sets up the GDirections load request, it won’t be able to get the results of the request synchronously (immediately). The GDIrections object has to go away and make a HTTP request to Google so that it can work out the distance between the two points.

    What you need to do is put your code that uses the distance into the callback function that you passed to the load request:

    GEvent.addListener(directions, "load", function() {
                // this is a callback function that is called after 
                // the getDistance function finishes.
                var distance = directions.getDistance().html;
    
                // Have a distance now, need to do something with it.
                doSomethingWithTheDistanceWeGotBack (distance);
        });
    

    Here is an example of using GDirections load (getting the driving duration, not the distance, but the principle is the same):

    http://www.cannonade.net/geo.php?test=geo6

    You can find the source here:

    http://www.cannonade.net/geo6.js

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

Sidebar

Related Questions

I'm simply trying to get data from two sql server db tables using ado.net
In a WinApp I am simply trying to get the absolute path from a
Is there a distance calculation implementation using hadoop map/reduce. I am trying to calculate
I am simply trying to get hold of the UIImageView that was tapped on,
I'm brand new to Prolog. I am simply trying to get some output from
I am trying to get a simple demo started with ActiveMQ that will demonstrate
I'm trying to create simply connect with ActiveMQ using JNDI. I have: Queue named
I am trying to write a console app that simply lists the number of
I'm trying to calculate a distance between two sets of coordinate points in an
i'm simply trying to get the current date in PHP, but it's getting '07:45:23'

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.