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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T11:38:38+00:00 2026-06-08T11:38:38+00:00

var geocoder, map, point, fmtAdd, marker; function mapLoad() { geocoder = new google.maps.Geocoder(); var

  • 0
var geocoder, map, point, fmtAdd, marker;

function mapLoad() {
geocoder = new google.maps.Geocoder();
var myOptions = {
  zoom: 15,
  mapTypeControl: false, 
  mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map"), myOptions);
address="W3 6BY";
if(address){geocoder.geocode({'address':address}, geocodeResult);}
else{alert("Postcode Incorrect");window.close();}
}

function geocodeResult(results, status) {
if (status == 'OK' && results.length > 0) {
    point=results[0].geometry.location;
    map.setCenter(point);
    marker = new google.maps.Marker({map: map, position: point, draggable: true});
    geocoder.geocode({latLng:point},function(results, status){
          if(status == 'OK') {
            if(results.length == 0) {
            fmtAdd = 'None';
            } else {
            fmtAdd = results[0].formatted_address;
            }
          } else {
            fmtAdd = 'Error';
          }
          alert(fmtAdd); // shows the address
        });
          alert(fmtAdd); // says undefined;
} else {
  alert("Error: " + status);
}
}
mapLoad();

I want to show the formatted address from user’s input who are in the UK. But I don’t understand why the second alert is undefined? Didn’t I defined the variable “fmtAdd” at the first line?

  • 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-08T11:38:40+00:00Added an answer on June 8, 2026 at 11:38 am

    Your “second” alert is actually your first alert since it is executed first (geocode() is non blocking – it returns immediately).
    At that point you “defined” fmtAdd, but you didn’t initialize it.

    var foo; alert(foo);
    

    alerts undefined.


    answering comment:

    I thought it was a global variable, and once the geocode give
    an value to it, I can retrieve that value even out of the geocode
    function.

    This is correct. The variable is initialized once the callback function passed to geocode() sets a value to it. And exactly that happens. After that “event” you can retrieve the value from your global variable also outside of your function.

    The problem here is that you’re trying to retrieve the value from fmtAddr before your callback function has completed (or is even called).
    This is because geocode() is non-blocking. This means that it returns immediately, this is why you pass a callback function to geocode().

    What happens

    referring to this part of the code:

    geocoder.geocode({ latLng: point }, function (results, status) {
        if (status == 'OK') {
            if (results.length == 0) {
                fmtAdd = 'None';
            } else {
                fmtAdd = results[0].formatted_address;
            }
        } else {
            fmtAdd = 'Error';
        }
        alert(fmtAdd); // shows the address
    });
    alert(fmtAdd); // says undefined;
    

    In chronological order:

    1. you call geocode(), passing a callback to it
    2. geocode() starts an asynchronous request to the google servers and returns immediately
    3. alert(fmtAdd); // says undefined;
    4. the asynchronous request completes and calls your callback function
    5. your callback function sets fmtAddr

    what you need to do

    execute your application in the correct order:

    • Create a function which does whatever you want to do with the formatted address.
    • Call this function from your callback. That is, after you set fmtAdd

    (actually better would be to pass the formatted address directly to this function as a parameter, without using global variables)

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

Sidebar

Related Questions

$(function(){ //Location was set. Perform actions. $(#geocodesubmit).click(function(){ var geocoder = new google.maps.Geocoder(); geocoder.geocode( {
Here is the Listener I'm adding - var map; var geocoder; function initialize() {
I have var marker = geo.geocode({address: adr}, geoc); where function geoc(results, status) {... I
var ref1 = new Firebase(http://gamma.firebase.com/myuser/123,456); ref1.set(123,456); var on1 = ref1.on(value, function(snapshot) { console.log(snapshot.val()); });
Hi I am having issues inserting a map from google maps and using the
Im trying out the Google Maps API Geocoding feature.The map loads fine, but im
I'm implementing Google Maps into a new project and one page should return a
I'm changing the center point on a google map by either two dropdown fields,
I am using Google Maps V3 API and using its Geocoder to do reverse
I want to remove an individual marker from Google map. I am using version

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.