0

Can someone please explain to me the following behaviour:

function getLatLong() { var geocoder = new google.maps.Geocoder(); var result = ""; geocoder.geocode ( { 'address': "London", 'region': 'uk' }, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { result = results[0].geometry.location; alert('Im called second'); } else { alert("Geocode was not successful for the following reason: " + status); } }); alert('Im called first'); return result; } 

How is the second alert message being called before the first alert? I have an issue whereby I'm trying to return to value of the assigned variable 'result' but it keeps returned as an empty string even though it does get assigned a value from results[0].geometry.location. I have a horrible feeling I'm missing something very obvious here :/

2 Answers 2

1

geocoder.geocode() is an asynchronous method, meaning that it returns immediately without blocking, but runs the specified function only once the geocoding call (presumably to Google's geocoding service) has completed.

What is happening is that the alert('Im called first') call is called before the other call has completed, most likely because the geocoding call has to go over the internet. The order of these two calls can vary, purely based on how long the geocoding takes to complete.

To solve this, you cannot return the result from this function. Instead, you need to supply a function to be called when the geocoding is complete to act as a callback so that you can then use the now filled-in result.

e.g.

function getLatLong(completeCallback) { var geocoder = new google.maps.Geocoder(); geocoder.geocode ( { 'address': "London", 'region': 'uk' }, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { var result = results[0].geometry.location; completeCallback(result); } else { alert("Geocode was not successful for the following reason: " + status); } }); } 
Sign up to request clarification or add additional context in comments.

Comments

1

oThe reason is that the call the geocoder.geocode uses a callback to deliver the results. The call to geocoder.geocode only lasts long enough to send out a request to the geocoder service, and then execution continues to the next (non-callback) line, which is the "Im called first" alert. All of the code in the callback is "saved for later" and invoked when the geocoder response is received.

You will have to write your code accordingly. Instead of returning the result from your original function call, you will have to take the result inside the callback and continue processing from there. It can make javascript control flow a little hard to follow.

1 Comment

Thanks for the response. I'm now just coding the processing in the first callback and it works great! Will look to chain the callbacks for clarity like the above answer demonstrates.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.