0

I hope you can help. Ive made a function that receives a lnglat point object and returns just the town. I can get it to print the correct town in the console.log but it doesnt return the data back from the function.

I know its going to be a basic error but can someone have a look at the code and let me know please.

Thanks in advance.

function getTownFromPoint(point){

    var geocoder ;
    var geocoder = new google.maps.Geocoder();
    var townset = false;
    mylocation = "";

    geocoder.geocode({latLng: point}, function(results, status) {

        if (status == google.maps.GeocoderStatus.OK) {

            if (results[0]) {

                var components=results[0].address_components;
                for (var component=0;component<(components.length);component++){
                    if(components[component].types[0]=="country" & !townset){
                        mylocation = components[component].long_name;
                    }

                    if(components[component].types[0]=="postal_code" & !townset){
                        mylocation = components[component].long_name;
                    }

                    if(components[component].types[0]=="locality"){
                        mylocation = components[component].long_name;
                        townset = true;
                        console.log(mylocation);
                    }
                }
            }
        }
    });
    return(mylocation);
}
beresfordt
  • 5,088
  • 10
  • 35
  • 43

3 Answers3

1

Geocoder is asynchronous - You are returning the value before you value is set.

It has been answered here:
Waiting for google maps geocoder?

Community
  • 1
  • 1
Stem
  • 56
  • 4
0

That's because geocode is an ajax call and they are asynchronous. You need to provide a callback function or use a a promise to get the data. Since you're not using jQuery by the looks of your question a callback might be easier:

Here's a simplified version of your code with an example of how the callback function can be used:

// we pass in the callback as a function parameter
function getTownFromPoint(point, callback) {

  geocoder.geocode({
    latLng: point
  }, function (results, status) {
       var myLocation = results.myLocation;

       // then we call the callback with the myLocation variable
       callback(mylocation);
     }
    );

}

// call the function with the point variable and the function
//  we will use for our callback
getTownFromPoint(1.223, function (myLocation) {
  console.log(myLocation)
});
Andy
  • 61,948
  • 13
  • 68
  • 95
  • Thanks for your reply. struggling to get my head around it a little.. so i see you are using console.log to show the result.. if i want to pass that out to a variable thats outside the function, how do i get it out? –  Mar 24 '15 at 12:28
  • So, all you're doing is passing in a function but not calling it until a specific moment along with the `myLocation` variable. When it gets called the variable is passed in as an argument and you can `console.log` it or whatever. [Here's a longer explanation.](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call/14220323#14220323) – Andy Mar 24 '15 at 12:31
  • @CraigArmitage, basically you can't very easily, so you may have to restructure you code so that callbacks become easier to use. – Andy Mar 24 '15 at 12:37
0

The problem you're facing is that you're treating the geocoder.geocode function as immediately completing before you do the return result. What's really happening is that the geocoder.geocode is triggered, then you get an immediate return of result. Because the asynchronous result has most likely not returned, your result is empty. Think of the geocoding result as a push, not a pull. The storeResult function, not shown, is whatever code you need to do to save the information. Because you're combining a result with an error string, you have to handle that in your storeResult function. As an alternative, you can have a status in the result that indicates succcess or failure.

store the result:

 storeResult(result);

use this function inside your function. this will solve your problem

Anshul Kalra
  • 198
  • 3
  • 13