You cannot do it. You have an asynchronous call to google geocoder, which means that you cannot return getAddr results. Instead, you should do something like this:
getAddr = function(addr, f){ if(typeof addr != 'undefined' && addr != null) { geocoder.geocode( { address: addr, }, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { f(results); } }); } return -1; }
And then you use in your code:
getAddr(addr, function(res) {
EDIT: if you want, you can also add another status confirmation:
getAddr = function(addr, f){ if(typeof addr != 'undefined' && addr != null) { geocoder.geocode( { address: addr, }, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { f('ok', results); } else { f('error', null); } }); } else { f('error', null); } }
And you can use it like this:
getAddr(addr, function(status, res) {
source share