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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:24:01+00:00 2026-06-03T13:24:01+00:00

so I’ve run into a problem recently, and maybe you guys can help. So

  • 0

so I’ve run into a problem recently, and maybe you guys can help.

So to start off, I’ve created website and a marker, and I’m trying to retrieve the center point to reverse-geocode the address.

My code is below :

function ReverseGeocode(lat, lng)
{
    var latlng = new google.maps.LatLng(lat, lng);
    var geocoder = new google.maps.Geocoder();
    geocoder.geocode({"latLng": latlng}, function(results, status)
    {
        if (status != google.maps.GeocoderStatus.OK)
        {
            alert("Geocoding has failed due to "+ status);
        }
        address = results[0].formatted_address;
    });
}

The problem I’m having here, is when I try to pass “address” back out (address is right now a global variable) all I get is “undefined”.

Here’s the code where I’m trying to pass it back out:
sendString += ‘&lat=’ + lat;
sendString += ‘&lng=’ + lon;
ReverseGeocode(center.lat(), center.lng());
alert(“”+address);

sendString += '&address=' + address 
var currentLang = "en"
sendString += '&phone=' + document.getElementById("number").value;
sendString += '&email=' + document.getElementById("email").value;
sendString += ($("prefsms").checked)?'&contactMethod=sms':'&contactMethod=email';
sendString += '&serviceType=' + document.getElementById("serviceType").value;
sendString += '&language=' + currentLang;
alert(""+sendString);

In my alert box, all I get is “undefined”. Yet, if I add another alert box INTO the ReverseGeocode function, I’ll get the address in an alert box, but this occurs AFTER the alert box in the outside function.

Any ideas as to what’s going on? I would have thought that the alert box inside the ReverseGeocode function would go first, not the other way around.

Thanks!

  • 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-03T13:24:03+00:00Added an answer on June 3, 2026 at 1:24 pm

    As Heitor Chang said, Geocoding is asynchronous – so when you try to return the address, it get’s returned to the function you pass as a callback to geocoder.geocode(). Confused? see this:

    function ReverseGeocode(lat, lng)
    {
        var latlng = new google.maps.LatLng(lat, lng);
        var geocoder = new google.maps.Geocoder();
        geocoder.geocode({"latLng": latlng}, function(results, status)
        {
            if (status != google.maps.GeocoderStatus.OK)
            {
                alert("Geocoding has failed due to "+ status);
            }
            return results[0].formatted_address; // this is how you might've been returning (i am just assuming since you didn't provide any code that returns address.
        });
    }
    

    Now you can see that it gets returned to the function you are passing to geocoder.geocode()

    What you should be doing is use callbacks – you are passing one here, probably without realising it – accept a callback as the third argument to ReverseGeocode function and when you get the result as OK, call the callback and return the address. Here’s how:

    function ReverseGeocode(lat, lng, cb)  // cb - callback, a function that takes the address as an argument.
    {
        var latlng = new google.maps.LatLng(lat, lng);
        var geocoder = new google.maps.Geocoder();
        geocoder.geocode({"latLng": latlng}, function(results, status)
        {
            if (status != google.maps.GeocoderStatus.OK)
            {
                alert("Geocoding has failed due to "+ status);
            }
            cb(results[0].formatted_address); // call the callback passing to it the address and we're done.
        });
    }
    

    How to use it? This way:

    ReverseGeocode( LAT, LNG, function(address) {
        // do something with the address here. This will be called as soon as google returns the address.
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am currently running into a problem where an element is coming back from
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
Specifically, suppose I start with the string string =hello \'i am \' me 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.