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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:06:39+00:00 2026-05-14T02:06:39+00:00

I am trying to build a little javascript class for geocoding addresses trough Google

  • 0

I am trying to build a little javascript class for geocoding addresses trough Google Maps API. I am learning Javascript and AJAX and I still can’t figure out how can I initialize class variables trough a callback:

    // Here is the Location class, it takes an address and 
    // initialize a GClientGeocoder. this.coord[] is where we'll store lat/lng
    function Location(address) {
        this.geo = new GClientGeocoder();
        this.address = address;
        this.coord = [];                        
    }

    // This is the geoCode function, it geocodes object.address and 
    // need a callback to handle the response from Google
    Location.prototype.geoCode = function(geoCallback) { 
        this.geo.getLocations(this.address, geoCallback); 
    }

    // Here we go: the callback. 
    // I made it a member of the class so it would be able 
    // to handle class variable like coord[]. Obviously it don't work.
    Location.prototype.geoCallback = function(result) {
        this.coord[0] = result.Placemark[0].Point.coordinates[1];
        this.coord[1] = result.Placemark[0].Point.coordinates[0];
        window.alert("Callback lat: " + this.coord[0] + "; lon: " + this.coord[1]);
    }

    // Main
    function initialize() {
        var Place =  new Location("Tokyo, Japan");
        Place.geoCode(Place.geoCallback);
        window.alert("Main lat: " + Place.coord[0] + " lon: " + Place.coord[1]);
    }

    google.setOnLoadCallback(initialize);

Thank you for helping me out!

EDIT

Thanks TJ for your reply. I read your example and your post — things are getting much clearer. But I still have an issue. Have a look:

function bind(context, func) {
    return function() {
        return func.apply(context, arguments);
    }
}
function Location(address) {
    this.geo = new GClientGeocoder();
    this.address = address;
    this.coord = [];                        
}

Location.prototype.geoCode = function(callback) { 
    this.geo.getLocations(this.address, callback); 
}

Location.prototype.geoCallback = function(result) {
    this.coord[0] = result.Placemark[0].Point.coordinates[1];
    this.coord[1] = result.Placemark[0].Point.coordinates[0];
    // This alert is working properly, printing the right coordinates
    window.alert("I am in geoCallback() lat: " + this.coord[0] + "; lon: " + this.coord[1]);
}

function initialize() {
    var Place =  new Location("Tokyo, Japan");
    Place.geoCode(bind(Place, Place.geoCallback));
    window.alert("I am in initialize() lat: " + Place.coord[0] + "; lon: " + Place.coord[1]);
}

Why the alert in initialize() pops before the alert in geoCallback(), printing an undefined/undefined?

  • 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-14T02:06:39+00:00Added an answer on May 14, 2026 at 2:06 am

    What you need to do is ensure that this is set correctly in the callback. This is sometimes called “binding”. Prototype provides Function#bind for this, but it’s easy enough to do if you don’t use Prototype — define a function that will do the binding for you:

    function bind(context, func) {
        return function() {
            return func.apply(context, arguments);
        }
    }
    

    and then use it in your initialize call:

    function initialize() {
        var Place =  new Location("Tokyo, Japan");
        Place.geoCode(bind(Place, Place.geoCallback)); // <= Change is here
        window.alert("Main lat: " + Place.coord[0] + " lon: " + Place.coord[1]);
    }
    

    (Although I think I’d suggest refactoring a bit so that the caller of geoCode doesn’t have to provide the callback at that level.)

    What bind above does is create a closure (a function) that, when called, will turn around and call the function you gave with this set to the context you gave, passing on any arguments that were given. (This is done above via Function#apply, which is a standard part of JavaScript.) You’d normally want to define bind at a fairly high level (page level or within your scoping function if you use one [which is a good idea]) to avoid having the generated functions closing over more data than necessary.

    Here’s a post in my anemic blog about this in a bit more detail.


    Regarding your edit: That’s actually a completely different question. By default, Ajax calls are asynchronous (which is why Google wants you to provide a callback function). So your code requests the data via getLocations, but that request is processed asynchronously and your code continues. The very next thing your code does is display the values you don’t have yet. At some later time, the request will complete and the values will be updated, but by then your code has finished. You’d want to move the alert (more generically, move your code processing the result) into a callback.

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

Sidebar

Related Questions

I am trying to build a little tool using google maps API. I thought
I'm trying to build my little app for cydia submission ... but I can't
I'm trying to build a small class-like container that will make it a little
I am trying to build a little app using the Foursquare API. require 'rubygems'
I'm trying to build myself a little helper library. first, for learning purposes, then
I'm trying to build a little calendar in JavaScript. I have my dates working
I am trying to build a little search script, but can't seem to stop
I'm trying to build a little calendar app sort of like Google Calendar. I'm
I'm trying to build a little Google Pagespeed client in Node, but I'm struggling
I'm trying to build a little renaming program to help save me time in

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.