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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:31:36+00:00 2026-06-03T06:31:36+00:00

I have a function in my map.js file called getNameOfCity() (The function is below)

  • 0

I have a function in my map.js file called getNameOfCity() (The function is below) It is called from my actions.js file by doing the following var city = getNameOfCity(); I then alert city and it says undefined.

Here is the function in map.js

function getNameOfCity() {
    geocoder.geocode({'latLng': map.getCenter()}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            if (results[0]){
                jQuery.each(results[0].address_components, function(key, value){
                    if(value.types[0] == 'locality') {
                        return value.long_name;
                    }
                });
            }
        } else {
            return false;
        }
    });
}

When I alert value.long_name just before I return the correct city is alerted. But when I alert city back from where the function was called it returns undefined. Any ideas on why?

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-03T06:31:38+00:00Added an answer on June 3, 2026 at 6:31 am

    as Adam said, the problem is you are returning to the anonymous function. do this instead:

    function getNameOfCity() {
        var city = '';
        geocoder.geocode({'latLng': map.getCenter()}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                if (results[0]){
                    jQuery.each(results[0].address_components, function(key, value){
                        if(value.types[0] == 'locality') {
                            city = value.long_name;
                        }
                    });
                    return city;
                }
            } else {
                return false;
            }
        });
    }
    

    UPDATE
    The callback way, as you were suggested…

    function getNameOfCity(callback) {
        geocoder.geocode({'latLng': map.getCenter()}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                if (results[0]){
                    jQuery.each(results[0].address_components, function(key, value){
                        if(value.types[0] == 'locality') {
                            callback(value.long_name); // call the function, pass the city name.
                        }
                    });
                }
            } else {
                callback(false); // not found? pass false instead.
            }
        });
    }
    

    UPDATE 2
    You can now use this function to get the city name this way:

    getNameOfCity(function(city) {
        alert(city);   // or do something less useless.
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a shell function that is called from inside my map function. The
I have a function in jquery: $('#map').click(function(e) { var posX = $(this).offset().left, posY =
I have the following RewriteMap function: RewriteMap map_company txt:/var/www/vhost/domain.com/httpdocs/map_company.txt I am trying to rewrite
I am having trouble accessing a Javascript function from my code behind file. I
I have a file context.ml where a map is defined module CtxMap = Map.make(struct
I have a file called text.txt that contains: <li id=unic1>some text</li> <li id=unic2>some text</li>
In my code below, I have a function that asynchronously loads all files necessary
Okay, I have a function which reads a xml file and creates controls using
I have a downloader function that downloads multiple files parallely. I use multiprocessing.Pool.map_async in
this is my code and have a error: $('#map_canvas').mouseover(function(e){ console.log(e.offset().left+' '+e.offset().top) }) thanks i

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.