geo = function(options){
geocoder.geocode( options, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var x = results;
alert('pear');
return x;
} else {
return -1;
}
});
}
getAddr = function(addr){
if(typeof addr != 'undefined' && addr != null) {
var blah = geo({ address: addr, });
alert('apple');
return blah;
}
return -1;
}
So when I call getAddr I get undefined, also apple is alerted first and then pear. I realize that google maps geocodes asynchronously, but is there a way to make this work?
You will not be able to do it that way. You have an asynchronous call to google’s geocoder, which means you will not be able to have the getAddr return the results. Instead you should do something like this:
And then you use in your code like that:
EDIT: If you want to you could also add more status validation:
And you can use it like that: