google maps api 3 - JavaScript - Global variable showing as undefined -


there many threads topic still haven't been able find suitable answer case...

here i'm trying do:

1) define function geocodes google maps api v3

2) call function address in question in order geocode particular address

the code works fine if place

    alert(address);  

after

    address = results[0].geometry.location; 

as need global variable, unfortunately not option. when trying use 'address' variable global function, value 'undefined'.

<!doctype html> <html> <head>   <script src="http://maps.googleapis.com/maps/api/js?sensor=false"       type="text/javascript"></script> </head>    <script type="text/javascript">     var geocoder;     var address;       address = 'frankfurt, germany';    function codeaddress() {     geocoder = new google.maps.geocoder();     geocoder.geocode( { 'address': address}, function(results, status) {   if (status == google.maps.geocoderstatus.ok) {  address = results[0].geometry.location;     } else {     alert("geocode not successful following reason: " + status);       }     });   }  codeaddress(address); alert(address);    </script>   </body> </html> 

anybody able me this? staring @ code doesn't seem me anywhere. appreciate available, thanks!!!

the problem geocode asynchronous, no?

that means geocode callback invoked "at point later" after outside alter in original post first called. (it should not alert undefined first set particular string, i'm ignore statement.)

function codeaddress() {   geocoder = new google.maps.geocoder();   geocoder.geocode( { 'address': address}, function(results, status) {     if (status == google.maps.geocoderstatus.ok) {       // use address *after callback success* , rid of       // global variable if possible - note how it's passed here.       // can use `debugger;` stop here , check value       // make sure set appropriate.       var address = results[0].geometry.location;       alert(address);       dootherstuffwithaddress(address);     } else {       alert("geocode not successful following reason: " + status);     }   }); }  function dootherstuffwithaddress (address) {    // don't need no stinking globals } 

see how return response ajax call?:

the in ajax stands asynchronous. means sending request (or rather receiving response) taken out of normal execution flow. in example, $.ajax returns , next statement, return result;, executed before function passed success callback called.

and specialized context:

the geocode function asynchronous. means sending request (or rather receiving response) taken out of normal execution flow. in example, geocode returns , next statement, alert(address), executed before function passed success callback called.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -