2

I have a function, in an external .js file which is supposed to take address string and then return latitude and longitude. It returns nothing. Can someone help please?

I also would like to ask; in javascript, when you call an async function from within another function; does the async function terminate as soon as the outside function terminates?

Here is the code, could anyone shed light on why this does not return anything? Thanks.

function FindLatLong(address)
{
        var geocoder = new google.maps.Geocoder();

        geocoder.geocode({ 'address': address }, function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                var lat = results[0].geometry.location.lat();
                var lng = results[0].geometry.location.lng();
                return { Status: "OK", Latitude: lat, Longitude: lng };
            }
        });
}

(I of course include the google maps js lib on top)

I basically do not get an OK result from geocoder; the same code works if I do not put it inside FindLatLong function, and instead just embed it in place where I call FindLatLong i.e. the code above does not work when put inside FindLatLong function, but works if I use it as part of the calling javascript code. The function does get called, Geocoder() does get called, but 'OK' result is not returned, in fact no result is resturned, not even error from google.

address string is "London, UK"

Mike
  • 214
  • 1
  • 3
  • 13
  • Yes, it's asynchronous. You cannot `return`. The outer function has already returned before the async one "finished" and calls the callback. – Bergi Aug 06 '14 at 18:04
  • jfriend00 is wrong. I looked at the link, read the long responses, and no the above issue/question is not duplicate. Although the problem being faced is related to async functions; a link which is an introduction to async programming and multiple ways to handle it - is not the 'solution' to the above problem. It is like sending a link to a book on the issue. For a person who may not know the exact nature of the problem, a specific answer is helpful. – Mike Aug 07 '14 at 10:49
  • Bergi, can you provide an example implementation of the above code? thanks. – Mike Aug 07 '14 at 11:06
  • I think the nature of the problem is well described in that post @jfriend linked (see also [this one](http://stackoverflow.com/q/23667086/1048572)). It might not be an exact duplicate, but whether the async function is `xhr.onload` or `geocoder.geocode` is not much difference. Have you tried implementing a callback solution? – Bergi Aug 07 '14 at 11:14
  • I did however find an exact duplicate now. Hope it helps. – Bergi Aug 07 '14 at 11:15
  • Thank you Bergi for your very quick response; and pardon my ignorance! I can implement the answer/solution but if I understood it better, it would be very helpful. I understand the reasons and the concept of callbacks. May be my issue is that- what makes something a callbback (syntax?). In the answer below, the line: callback({ Status: "OK", Latitude: lat, Longitude: lng }); means what? Where is 'callback' defined in this example? Is 'callback' just a parameter of FindLatLong; if yes what makes it a function? And also am I correct the one never 'returns' from within an async function? – Mike Aug 07 '14 at 11:47
  • *What makes something a [callback](https://en.wikipedia.org/wiki/Callback_(computer_programming))?* - the fact that you're passing a function (as an argument) to a function/expecting a parameter of your function to be a function that you call back. *What makes it an **async** callback?* - the API that you're using. It is documented there that the callback will be called somewhen in the future, not before the function returns. See also my comment on the answer. – Bergi Aug 07 '14 at 14:58

1 Answers1

3

The method is async, so use a callback!

function FindLatLong(address, callback)
{
    var geocoder = new google.maps.Geocoder();

    geocoder.geocode({ 'address': address }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            var lat = results[0].geometry.location.lat();
            var lng = results[0].geometry.location.lng();
            callback({ Status: "OK", Latitude: lat, Longitude: lng });
        }
    });
}

And call the function:

FindLatLong(address, function(data) {
    console.log(data);
});
tymeJV
  • 103,943
  • 14
  • 161
  • 157
  • tymeJV, Thank you very much for your response. I have read stuff on the link someone posted on the top. I understand that the outer function returns before geocoding is done. What I do not understand is - 1) Is it the case that in my code above, I should never 'return' from the FindLatLng function? 2) In your code what is 'callback', is it a function i.e. what makes it a callback and where is the definition of this function? Thanks! – Mike Aug 07 '14 at 11:08
  • `callback(…)` means that this is the point where the `callback` function, given to you by the parameter, is called. *What makes it a function?* - the value that is passed. *Where is this function defined?* - In the example call (second snippet) you see a function expression for the second argument. – Bergi Aug 07 '14 at 15:01