T.H.Naseri
T.H.Naseri

Reputation: 57

How store latitude and longitude from navigator.geolocation.getCurrentPosition() into a variable before returning the variable in javascript

I have written the following JavaScript function to return latitude and longitude values so I am trying to store them into variables and return those variable, but since navigator.geolocation.getCurrentPosition() function is asynchronous return action happens before that and my function always returns undefined. Can anyone show me a way to return values after the asynchronous function?

function getLocation() {
    let lat = 0
    let long = 0
    if(navigator.geolocation) {
        navigator.geolocation.getCurrentPosition( function(position) {
            console.log(position.coords.latitude, position.coords.longitude)
            lat = position.coords.latitude
            long = position.coords.longitude
      
            //console.log("LATLONG1: ", lat, long) //test..
        })
    }
  
    return [lat,long]
}

Upvotes: 1

Views: 3466

Answers (4)

T.H.Naseri
T.H.Naseri

Reputation: 57

Thanks a lot @Levi Ermonaites De Freitas and @spacing for your help. After trying a lot to get a solution, though I didn't get the absolute answer, I came to conclusion that I would create a promise out of the navigator.geolocation.getCurrentPosition() function and resolve the values which I want and use the promise followed by then() to use those values (lat, long) wherever I need to. The following code will make my statement clearer.

// Creating a promise out of the function
let getLocationPromise = new Promise((resolve, reject) => {
    if(navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function (position) {

            // console.log(position.coords.latitude, position.coords.longitude) //test...

            lat = position.coords.latitude
            long = position.coords.longitude

            // console.log("LATLONG1: ", lat, long) //test...

            // Resolving the values which I need
            resolve({latitude: lat, 
                    longitude: long})
        })

    } else {
        reject("your browser doesn't support geolocation API")
    }
})

// Now I can use the promise followed by .then() 
// to make use of the values anywhere in the program
getLocationPromise.then((location) => {
    console.log(location.latitude)
}).catch((err) => {
    console.log(err)
})

Upvotes: 3

Following the reasoning of @spacing, but with some modifications:

function getLocation() {
        let lat = 0
        let long = 0
        if(navigator.geolocation) {
            navigator.geolocation.getCurrentPosition( async function(position) {
                console.log(position.coords.latitude, position.coords.longitude)
                lat = await position.coords.latitude
                long = await position.coords.longitude
          
                //console.log("LATLONG1: ", lat, long) //test..
            })
        }
      
        return [lat,long]
    } 

Your callback function should be an async function, since that is it that is trying to access the values that will be returned by getCurrentPosition

Upvotes: 1

spacing
spacing

Reputation: 780

I think async/await should solve the issue here

    async function getLocation() {
        let lat = 0
        let long = 0
        if(navigator.geolocation) {
            await navigator.geolocation.getCurrentPosition( function(position) {
                console.log(position.coords.latitude, position.coords.longitude)
                lat = position.coords.latitude
                long = position.coords.longitude
          
                //console.log("LATLONG1: ", lat, long) //test..
            })
        }
      
        return [lat,long]
    }

Upvotes: 1

Since what I understood is that you wanna return the lat and long values from your function, you should just get rid of this "return" statement in the middle of the "if" statement.

Upvotes: 1

Related Questions