2

So I've been having this issue for the past couple of hours. I'm trying to get a user's location by using the navigator object, and I'm able to get the lat and long just fine, but when I try to return it and use it as a variable for a Google Maps LatLng object, it returns as undefined.

Here is the code:

function getCurrentLat(){
    var lat
    if(navigator.geolocation){

        navigator.geolocation.getCurrentPosition(function(position) {
            lat = position.coords.latitude;
            alert(lat);
        });
        alert(lat);
    }else{
        console.log("Unable to access your geolocation");
    }
    return lat;
}

The first alert inside the getCurrentPosition function will show the correct latitude, however, the second one outside the function shows as undefined. How can I get the variable to show correctly outside of the getCurrentPosition() function?

Ajax413
  • 21
  • 1
  • 4
  • `return lat;` maybe instead of the `return(lat);` ... what is that anyway? – Tim Vermaelen Nov 16 '14 at 05:28
  • Whoops, that was a typo. Using return lat; doesn't work as well. – Ajax413 Nov 16 '14 at 05:57
  • Does this answer your question? [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – Sebastian Simon May 23 '20 at 14:00

1 Answers1

0

getCurrentPosition takes a callback, and within that function you end up with a different internal scope (and may not execute immediately). So the only way you can do this is to either return a future or take in a callback, e.g.:

function getCurrentLat(callback){
    if(navigator.geolocation){

        navigator.geolocation.getCurrentPosition(function(position) {
            callback(position.coords.latitude);
        });
    }else{
        console.log("Unable to access your geolocation");
    }
}

otherwise, it's very clear why lat would be undefined, since getCurrentPosition will not even have a chance to execute and call the callback until after the function exits.

Jeff Tratner
  • 16,270
  • 4
  • 47
  • 67
  • Makes sense, I figured it was a scope issue, just couldn't figure out how to pass it. For the callback function, what exactly is passed to the main getCurrentLat() function? Sorry I'm a little new to callbacks so I'm not 100% sure what the best way to grab the value from said callback is. Is there a way I can set getCurrentLat()'s return value to position.coords.latitude? – Ajax413 Nov 17 '14 at 01:22
  • well in this case, it'd be passed the value of `position.coords.latitude`. e.g., something like `getCurrentLat(function(latitude) { alert('Your latitude is ' + latitude); })` – Jeff Tratner Nov 17 '14 at 03:50
  • Hey, I have a similar issue but it's not clear as to how to get the desired results from callback. I'm new to callbacks so if answer was a bit specific then it would have helped. – Rahul Sonwanshi May 12 '20 at 12:50