1

Im trying to get the current position of the user into an object variable but it keeps resetting on it self.

var UserCoords = { lat: '', lng: '' };
function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);

    } else {
        alert("Geolocation is not supported by this browser.");
    }
}

function showPosition(position) {
    UserCoords.lat = position.coords.latitude;
    UserCoords.lng = position.coords.longitude;
    console.log(UserCoords);//Works
}

function getUserCoords() {
    getLocation();
    console.log(UserCoords);//Returns lat and lng empty
    OtherFunction(UserCoords.lat, UserCoords.lng);
}

I also tried:

function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function (position) {
            UserCoords.lat = position.coords.latitude;
            UserCoords.lng = position.coords.longitude;
            console.log(UserCoords);//Works
        });
}

But neither works.

I want to do this because i need to use the users position in some other functions and i would like to not have to call geolocation every time.

UPDATE:

jsfiddle: http://jsfiddle.net/9fvcqcvz

Jemomi_DK
  • 41
  • 7
  • put it in fiddle please... – EugenSunic Aug 06 '15 at 14:27
  • 1
    possible duplicate of [unable to cope with the asynchronous nature of navigator.geolocation](http://stackoverflow.com/questions/2707191/unable-to-cope-with-the-asynchronous-nature-of-navigator-geolocation) – Andreas Aug 06 '15 at 14:34
  • @eugensunic - Added jsfiddle link. – Jemomi_DK Aug 07 '15 at 07:51
  • @Andreas - It doesn't work the way i want it to because it still resets the values of my variable when i try to call it after the function that has "loc". – Jemomi_DK Aug 07 '15 at 07:52
  • @Andreas - I looked at it and i can use it to workaround the problem but i still dont like having to call the function every time i need the position for something. I think it would be faster for the page if i could just get the content of a variable – Jemomi_DK Aug 07 '15 at 10:06

1 Answers1

0

It's not populated yet, when you try to access it. The showposition callback function will run only after the browser gets the coords (after the user accept it) So any function call with the UserCoords should be called AFTER this.

  • How would i go about doing that? – Jemomi_DK Aug 07 '15 at 08:07
  • Check out this link as Andreas suggested: http://stackoverflow.com/questions/2707191/unable-to-cope-with-the-asynchronous-nature-of-navigator-geolocation – Miklós Drágán Aug 07 '15 at 09:27
  • i did and i can use it as a work around but i would really like it if i could get the positions into a variable i can just call to make it easyer for me. – Jemomi_DK Aug 07 '15 at 10:05
  • You can store it into a variable, but if you want to read it before it gets a value, it will fail. It is not guaranteed to get the location from the browser anyway. – Miklós Drágán Aug 07 '15 at 10:41
  • if i try to store it into a variable and then try to access it later it doesn't have any content. my code is already setup to handle if the user denys position or the browser doesn't support it (it's not shown in the snippet that i have shown here) – Jemomi_DK Aug 07 '15 at 11:01