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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:42:10+00:00 2026-05-23T20:42:10+00:00

I am newbie in Maps API, i am trying get the distance (in Km)

  • 0

I am newbie in Maps API, i am trying get the distance (in Km) from 2 adress. What is wrong with this code?

var mygc = new google.maps.Geocoder();

var locationOrigem;
var locationDestino;

var latOrigem  = 0;
var longOrigem = 0;

var latDestino  = 0;
var longDestino = 0;


mygc.geocode({'address' : 'Presidente Vargas 897, Centro RJ'}, function(results, status){

    locationOrigem = results[0].geometry.location;
    latOrigem   = results[0].geometry.location.lat();
    longOrigem  = results[0].geometry.location.lng();

});

mygc.geocode({'address' : 'Abelardo Bueno 3000, Barra da Tijuca RJ'}, function(results, status){

    locationDestino = results[0].geometry.location;
    latDestino  = results[0].geometry.location.lat();
    longDestino = results[0].geometry.location.lng();

});

alert(google.maps.geometry.spherical.computeDistanceBetween(locationOrigem, locationDestino));

By a unknown reason, var locationOrigem and locationDestino are “undefined” outside of anonymous functions ??? Why???

  • 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-23T20:42:12+00:00Added an answer on May 23, 2026 at 8:42 pm

    Have you included this script?

    <script type="text/javascript" src="http://maps.google.com/maps/api/js?libraries=geometry&sensor=false&language=en"></script>
    

    What happen when you run your code? Are you sure that your addresses were geocoded correctly? Are you sure that problem is in computeDistanceBetween. Try to dump all variables.

    UPDATE:

    Requests to the Geocoder are executed asynchronously. So request for distance calculation were run before you had got geocoded data. That is why your variables were undefined. You need to use callbacks to ensure that request is finished. Try this code

    var mygc = new google.maps.Geocoder();
    var locationOrigem;
    var locationDestino;
    var latOrigem  = 0;
    var longOrigem = 0;
    var latDestino  = 0;
    var longDestino = 0;
    
    mygc.geocode({'address' : 'Presidente Vargas 897, Centro RJ'}, function(results, status){
        locationOrigem = results[0].geometry.location;
        latOrigem   = results[0].geometry.location.lat();
        longOrigem  = results[0].geometry.location.lng();
        mygc.geocode({'address' : 'Abelardo Bueno 3000, Barra da Tijuca RJ'}, function(results, status){
            locationDestino = results[0].geometry.location;
            latDestino  = results[0].geometry.location.lat();
            longDestino = results[0].geometry.location.lng();
            console.log(locationOrigem);
            console.log(locationDestino);
            console.log(google.maps.geometry.spherical.computeDistanceBetween(locationOrigem, locationDestino));
        });
    });
    

    Also you need to check status of response. I haven’t include because it is not the subject.

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

Sidebar

Related Questions

I am a newbie to html/css/javascript and code like this really scares me: http://code.google.com/apis/maps/documentation/javascript/overlays.html#InfoWindows
i'm checking out some newbie Google Maps API tutorial code. They have an example
I'm trying to develop a Store Locator (using Google Maps v3 API), that's located
Newbie here (to C, objective-C, and iOS)... For this code in my AppDelegate.m, why
I'm a newbie in the Rails, but I can't figure this out. Rails maps
I'm using the google-maps-utility-library-v3 to incorporate a Google Earth API view to a Google
I'm trying to instantiate classes from Maps[String, Any], receiving through some json-rpc. So I
Newbie Scala Question: Say I want to do this [Java code] in Scala: public
I'm newbie in php mysql and google maps v3. I have a problems in
Newbie trying to figure out how to get a Node.js application to authenticate 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.