VikkyB
VikkyB

Reputation: 1450

Javascript change value of global variable

var longitude=1;
var latitude=1;

var geocoder =  new google.maps.Geocoder();
    geocoder.geocode( { 'address': Position}, function(results, status) {
          if (status == google.maps.GeocoderStatus.OK) {
            alert(results[0].geometry.location.lat());
            alert(results[0].geometry.location.lng());
            latitude = results[0].geometry.location.lat();
            longitude = results[0].geometry.location.lng();
            //alert("location : " + results[0].geometry.location.lat() + " " +results[0].geometry.location.lng()); 
          } else {
            alert("Something got wrong " + status);
          }
        });

I am trying to change the values of global variables latitude and longitude but not able to. I have looked up the way to assign values to global variables inside a function and I think I am doing that part right. But clearly there is something that I am missing. Please help.

Upvotes: 0

Views: 145

Answers (3)

maček
maček

Reputation: 77816

The function(results, status){ ... } bit is an asynchronous callback

The issue you're likely running into is that you're trying to access the longitude and latitude values before they're actually set

To confirm this, modify your callback to the following

// where you have these two lines
latitude = results[0].geometry.location.lat();
longitude = results[0].geometry.location.lng();

// add this line after
console.log(latitude, longitude);

You should see them just fine. Once you have that bit working, you could skip them altogether and do something like this

function doSomething(lat, lng) {
  console.log(lat, lng);
}

geocoder.geocode( { 'address': Position}, function(results, status) {

  // ...
  var loc = results[0].geometry.location,
      lat = loc.lat(),
      lng = loc.lng();

  doSomething(lat, lng);

  // ...

});

This way you can skip having latitude and longitude in the outer scope, too. Pretty handy!

Upvotes: 1

timetocode
timetocode

Reputation: 71

Try this code:

var longitude=1;
var latitude=1;

var geocoder =  new google.maps.Geocoder();
    geocoder.geocode( { 'address': Position}, function(results, status) {
          if (status == google.maps.GeocoderStatus.OK) {         
            latitude = results[0].geometry.location.lat();
            longitude = results[0].geometry.location.lng();
            alert(latitude + ', ' + longitude) // show the value of the globals
          } else {
            alert("Something got wrong " + status);
          }
        });

If that works correctly, then the answer is probably that the globals are being correctly set, but they simply haven't been set by the time other code attempts to use them.

If this occurs, it means that whatever code relies on the lat/long needs to wait until the geocode callback has finished and received data.

Upvotes: 0

tly_alex
tly_alex

Reputation: 113

I recommend you attach those two variable to the global window object.

Like: window. latitude and window.longitude

And the function trying to change the value is an async callback function, there might be local variable with the same name defined in that scope.

Attaching it to window should get you around that possibility.

Upvotes: 0

Related Questions