Hi I am working with google maps API
I have this huge code, so I dont want to scare away people with code
Basically this is what I need to do
I have a javascript(1) function that I call from Html after someone clicks a button,
from that function I do some processing to calculate the latitudes and longitudes and call a custom function which uses the latitudes and longitudes and calls a google maps function. I need to pass that the value from the google maps call back function back to the javascript function (1)
here is how it goes
————–File First.html—————-
<script type="text/javascript" src="functions.js"></script>
<script type="text/javascript">
function docalculation
{
.....some calculations for latlng......
var value = get_value(latlng);
}
</script>
<html>
.....html file....
<input type="button" value="Create Directions" onClick="docalculation()">
</html>
——————-file functions.js—————-
var return_val;
function get_val(latlng)
{
geocoder = new google.maps.Geocoder();
geocoder.geocode({'latLng': latlng}, function(results, status) {
return_val = (results[0].address_components[i].long_name);
alert(return_val); //here I get the value that I need in alert
}
alert (return_val); // here the value suddenly becomes undefined
return val; // hence returns undefined
}
Please help I am stuck here and could not find a solution even after 2 days of googling and trying everything I know.
any help appreciated thanks
The simplest solution that I am looking for would be some way to store the value of return_val into variable value
Thanks
That’s because
geocode()is asynchronous: it returns immediately but the callback function is invoked as soon as the response arrives. You need to accept a callback inget_val()and pass the value to this method instead of trying to return it (it’s impossible):Usage: