This is very simple issue, but unable to solve, i.e Variable scope in javascript for geoLocation values not working. Here is my code:
<script type="text/javascript">
var glat;
var glng;
var test = "Hiii";
function call(){
var lat;
var lng;
var geocoder = new google.maps.Geocoder();
var address = "Delhi";
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
lat = results[0].geometry.location.lat();
lng = results[0].geometry.location.lng();
}
glat = lat;
glng = lng;
alert("In: "+glat + glng+ "Test: "+test);
//All Values Displayed Fine..
});
alert("OUT: "+glat + glng+ "Test: "+test);
//glat and glng displayed as undefined..
}
</script>
Why, the values in OUT alert not displaying..?? How can I solve this..
Thanks in Advance..
Get the geo info is an async process, you should do such job with a callback.
Example: