I want to return a value from my function. When I call the function outside without returning nothing but just alerting the 'res' variable it alerts ok. But when I return the res variable from the function and then alert the entire function it alerts 'undefined'. Why does this happen? I want to mention again that when I alert the res variable everything alerts ok, just that I can not return from function.
function geocodeLatLng(location, method) {
var res;
var geocoder = new google.maps.Geocoder;
geocoder.geocode(method, function(results, status) {
if (status === 'OK') {
if (results[0]) {
if(method.address){
res = results[0].geometry.location
var marker = new google.maps.Marker({
position: res,
draggable:true,
map: map
});
}
else {
res = results[0].formatted_address
}
show_road(results[0].geometry.location);
} else {
window.alert('good');
}
} else {
window.alert('wrong: ' + status);
}
});
return res;
}
alert (geocodeLatLng(center, {'location': center}));