/    Sign up×
Community /Pin to ProfileBookmark

Functions, Callbacks and my Question

Hey everyone,

I have an issue with the concept of JavaScript that I don´t really understand.

[CODE]
var gloablx;

function doMapStuff(){

var address = “bla”;
var x;

geocoder(address, function(results){
addressLat1 = results[0].geometry.location.lat(); //Works
addressLng1 = results[0].geometry.location.lng(); //Works
x = addressLat1;
gloablx = addressLat1;
});
console.log(addressLat1); //NOT WORKING
console.log(x ); //NOT WORKING
console.log(gloablx ); //NOT WORKING
}

function geocoder(address, callback){

var geocoder = new google.maps.Geocoder();
geocoder.geocode({‘address’: address}, function (results, status){
if(status === google.maps.GeocoderStatus.OK){
console.log(results);
callback(results);
}else {
alert(‘Geocode was not successful for the following reason: ‘ + status);
}
});
}
[/CODE]

I´m trying to use result of my geocoder function outside of the call (Don´t really know how to name this), but it isn´t working. I have also tried to use Prototypes, Getter & Setter etc., but I can´t get a value. What I´m doing wrong? How do I have to rewrite the code, to make this working? I hope someone can help me here ?

Best regards

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@daveyerwinNov 03.2015 — the red part is a function

definition, it does not run until

called by the green code ...
[CODE]
geocoder(address, [COLOR="#FF0000"]function(results){
addressLat1 = results[0].geometry.location.lat(); //Works
addressLng1 = results[0].geometry.location.lng(); //Works
x = addressLat1;
gloablx = addressLat1;
}[/COLOR]);
console.log(addressLat1); //NOT WORKING
console.log(x ); //NOT WORKING
console.log(gloablx ); //NOT WORKING
}
function geocoder(address, callback){

var geocoder = new google.maps.Geocoder();
geocoder.geocode({'address': address}, function (results, status){
if(status === google.maps.GeocoderStatus.OK){
console.log(results);
[COLOR="#008000"]callback(results);
[/COLOR] }else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}
[/CODE]

your console.log s run immediately

after the function is defined and

before the data is returned from

the server, so at the time the

console.log s run the values you

log have not yet been set. They

are only set after the server

successfully returns data.
×

Success!

Help @Dev0x spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...