• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 324
  • Last Modified:

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

0
tablaFreak
Asked:
tablaFreak
  • 2
  • 2
1 Solution
 
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
 
tablaFreakAuthor Commented:
Ah yes, but of course! Thanks.
0
 
hieloCommented:
You are welcome.

Regards,
Hielo
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now