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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T16:42:37+00:00 2026-05-21T16:42:37+00:00

Hi I am working with google maps API I have this huge code, so

  • 0

Hi I am working with google maps API
I have this huge code, so I dont want to scare away people with code
Basically this is what I need to do

I have a javascript(1) function that I call from Html after someone clicks a button,
from that function I do some processing to calculate the latitudes and longitudes and call a custom function which uses the latitudes and longitudes and calls a google maps function. I need to pass that the value from the google maps call back function back to the javascript function (1)

here is how it goes

————–File First.html—————-

<script type="text/javascript" src="functions.js"></script>
<script type="text/javascript">
function docalculation
{
  .....some calculations for latlng......
  var value = get_value(latlng);
}
</script>
<html>
  .....html file....
  <input type="button" value="Create Directions" onClick="docalculation()">
</html>

——————-file functions.js—————-

var return_val;
function get_val(latlng)
{
  geocoder = new google.maps.Geocoder();
  geocoder.geocode({'latLng': latlng}, function(results, status) {

  return_val = (results[0].address_components[i].long_name);
  alert(return_val); //here I get the value that I need in alert
  }

alert (return_val); // here the value suddenly becomes undefined
return val; // hence returns undefined
}

Please help I am stuck here and could not find a solution even after 2 days of googling and trying everything I know.

any help appreciated thanks

The simplest solution that I am looking for would be some way to store the value of return_val into variable value

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-05-21T16:42:38+00:00Added an answer on May 21, 2026 at 4:42 pm

    That’s because geocode() is asynchronous: it returns immediately but the callback function is invoked as soon as the response arrives. You need to accept a callback in get_val() and pass the value to this method instead of trying to return it (it’s impossible):

    function get_val(latlng, cb) {
        geocoder = new google.maps.Geocoder();
        geocoder.geocode({
            'latLng': latlng
        }, function (results, status) {
            cb(results[0].address_components[i].long_name);
        });
    }
    

    Usage:

    get_value(latlng, function(longName) {
        /* do something */
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some C# code that generates google maps. This codes looks at all
I'm working with the google maps API, and testing a section which geocodes all
Update II I Figured this out. When using google maps api, the model must
I have an application that uses the Google Maps API to geocode distances between
I've been working for a few weeks now with the Google Maps API v3,
I am working on an Android app that utilizes the Google Maps API MapView,
I'm working on an advanced map application in Google Maps API V3. I'm using
I've been having a problem with Google maps API v.3. I have my canvas
I am playing around with the Google Maps API V3. I want to create
I'm currently working on a Google Maps project and am implementing a search function.

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.