0
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.

1
  • Alerts are giving the correct results but those values are not getting assigned to latitude and longitude. Commented Dec 2, 2013 at 4:24

3 Answers 3

1

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!

Sign up to request clarification or add additional context in comments.

2 Comments

Yes that is indeed the issue. Do I need to put some delay function to wait for the values to get assigned?
You don't need to artificially delay anything, but you need to make sure that whatever function you call is called within the Google Maps callback.
0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.