I want to save some info to the database , but should happen after user location function is done getting the current user location.
async function getImmediate() {
let coord;
navigator.geolocation.getCurrentPosition(
async position => {
const latitude = await JSON.stringify(position.coords.latitude);
const longitude = await JSON.stringify(position.coords.longitude);
coord = await position.coords.latitude;
},
error => Alert.alert(error.message),
{
enableHighAccuracy: true,
timeout: 20000,
maximumAge: 1000
}
);
return coord;
}
async function saveNow(){
let coordinates = await getImmediate();
//save to the database
console.log(coordinates)
}
i get null values and undefined instead.
i will be grateful for your help as it seems am not locating where the problem is quick...
You can use promises to achieve what you're trying to do. The below code should work and return the result:
function getImmediate() {
return new Promise((resolve, reject) =>
navigator.geolocation.getCurrentPosition(
position => {
const latitude = JSON.stringify(position.coords.latitude);
const longitude = JSON.stringify(position.coords.longitude);
resolve({ latitude, longitude});
},
error => Alert.alert(error.message),
{
enableHighAccuracy: true,
timeout: 20000,
maximumAge: 1000
}
)
);
}
async function saveNow(){
let coordinates = await getImmediate();
//save to the database
console.log(coordinates)
}