1

I have following code:

js load:

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>

js function:

<script type="text/javascript">

    var get_location;

    function get_google_latlng() {

        var geocoder =  new google.maps.Geocoder();
        geocoder.geocode( { 'address': 'iran'}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                window.get_location = results[0].geometry.location.lat();
            } else {
                window.get_location = status;
            }
        });

        return window.get_location;
    }

    var lat = get_google_latlng();

    alert(lat);
</script>

return Function is undefined

window.get_location command also does not work.

6
  • 2
    possible duplicate of How to return the response from an AJAX call? Commented Feb 28, 2014 at 9:52
  • What are you trying to achieve with window.get_location? What do you think it is/does? Commented Feb 28, 2014 at 9:58
  • try to use 'get_location' rather than 'window.get_location' Commented Feb 28, 2014 at 9:58
  • use alert('test'); inside the conditions and try to find which is going wrong Commented Feb 28, 2014 at 10:00
  • get_location command also does not work. Commented Feb 28, 2014 at 10:01

2 Answers 2

2

What you have is a problem with asynchronous functions. You don't have the values of the geocode method immediately, because you are making an ajax request and that takes time. Typical for a JavaScript newbie.

Callbacks and closures are the techniques that will make your life easier when programming JavaScript. I will suggest you to change your way of thinking, this ain't turbo-pascal anymore. This is JavaScript. async. Don't expect every function to immediately return results.

Example with callbacks:

// Ugly global variable
var get_location;

// Even more ugly global function
function get_google_latlng(callback) {

    var geocoder =  new google.maps.Geocoder();
    geocoder.geocode( { 'address': 'iran'}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            window.get_location = results[0].geometry.location.lat();
        } else {
            window.get_location = status;
        }

        // Now you invoke the callback to notify that the results are ready
        callback();
    });

    // This is absolutely unnecessary
    return window.get_location;
}

get_google_latlng(function(){

   // Only here we are sure the variable was actually written       
   alert(window.get_location);
});

One last thing, never-ever-ever declare functions, and variables directly under "window", the global object in JavaScript, this is an anti pattern that will give you headaches in the future.

Please learn how to make anonymous functions.

Sign up to request clarification or add additional context in comments.

2 Comments

I would like to receive the output.
You can do whatever you want inside of the callback. That's where you "receive" the output.
0

Try this code :

var get_location;
var geocoder =  new google.maps.Geocoder();
geocoder.geocode( { 'address': 'iran'}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            get_location = results[0].geometry.location.d;
            alert(get_location);
        }
});

The problem with your code was, alert was getting executed first and then the get location function.

1 Comment

I would like to receive the output. I do not want to alert.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.