Skip to content Skip to sidebar Skip to footer

Accessing Array From Outside Of Geocode Loop

I have managed to geocode upto 10 address and add a marker to the map but I need to access the geocode results outside of the geocode (codeAddress) function. I thought I could push

Solution 1:

When your codeAddress function returns, geocoder.geocode is still doing its job, which is asynchronous. When geocoder.geocode is done, it will invoke its callback, which you're passing as an anonymous function.

The solution I suggest is to make the callback function a parameter of codeAddress, and do whatever you need from that callback (or from other function called inside the callback), instead of using the global variables approach.

Solution 2:

It's all about scope in Javascript. If you want to access variables outside of the function, you must declare and create variables outside of the function scope and then update them.

Variables declared outside of the function scope will be available anywhere else within the script.

var def = "bombshell";
 function changer(){
     def = "changed value";
 }
 changer();
 alert(def);

See it here - http://jsfiddle.net/daveheward/kWn8b/

Post a Comment for "Accessing Array From Outside Of Geocode Loop"