Javascript global variable not getting set

Hello -

I'm trying to use Google Maps API to retreive Longitutde and Latitude coords in an Ajax call to a database, and in using global variables, for some reason my Longitude value keeps coming back undefined, though the Latitude value is getting set. If I set an alert within the geocoder function, a valid value is getting returned for Longitude - I'm just not seeing why when I try to check the value with an alert, it's showing up as undefined (it's a negative decimal/float value, btw)

Thanks
var fLng;
var fLat;
var geocoder = new GClientGeocoder();

function setLL(address) {
    var aLL, sLL;
    geocoder.getLatLng(
            address,
            function(point) {
                if (point) {
                    fLng = point.lng();
                    fLat = point.lat();
                 }
            }
          );
}

function SaveLongLat(){
	var sAddress = 'some address'
	setLL(sAddress);
	alert(fLng);
	alert(fLat);
         // save vals to db
}

Open in new window

tablaFreakAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

corvus187Commented:
Have you tried turning it off and then on again?
0
tablaFreakAuthor Commented:
How do you mean?
0
hieloCommented:
the reason for your "undefined" is because here:
var fLng;
var fLat;

those variables have NOT been assigned anything. So when you enter:
function SaveLongLat(){...}

those variables are still undefined. Your confusion is because as soon as you execute:
setLL(sAddress);

you expect these:
       alert(fLng);
        alert(fLat);

to show the values assigned in setLL() IMMEDIATELY. That is not going to happen because within setLL(), a remote request is sent via geocoder.getLatLng().

Basically, that is an asynchronous call. In other words, the remote request may take 10 seconds, but the browser does NOT wait for that call to finish/return. It just keeps executing the next statement immediately.

So when you call setLL(sAddress), that remote has NOT finished/completed, and hence the variables remain undefined.

If you look closer at geocoder.getLatLng(), the second paramater is an anonymous function. THAT function will get execute when the remote request finishes. SO THERE is where you need to assign those values and save to the db within that function as well

var fLng;
var fLat;
var geocoder = new GClientGeocoder();

function setLL(address) {
    var aLL, sLL;
    geocoder.getLatLng(
            address,
            function(point) {
                if (point) {
                    fLng = point.lng();
                    fLat = point.lat();
				
				alert(fLng + " " + fLat);
				
				//assign values to db here
                 }
            }
          );
}

function SaveLongLat(){
        var sAddress = 'some address'
        setLL(sAddress);
//        alert(fLng);
 //       alert(fLat);
         // save vals to db
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tablaFreakAuthor Commented:
Ah yes, but of course! Thanks.
0
hieloCommented:
You are welcome.

Regards,
Hielo
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.