I am trying to log the users location every x amount of seconds. In this specific code, it is 5 seconds. I am using watchPositionAsync. The code below I thought would log the latitude and longitude every 5 seconds however it does not log anything in the console at all.
I want to be sure it is updating and logging it in the console would confirm that. Let me know if you have any questions or need to see more code.
expo-location docs: https://docs.expo.dev/versions/latest/sdk/location/
import * as Location from 'expo-location';
function MyTabs() {
const [latitude, setLatitude] = React.useState(null);
const [longitude, setLongitude] = React.useState(null);
React.useEffect(() => {
(async () => {
let { status } = await Location.requestForegroundPermissionsAsync();
if (status !== 'granted') {
permissionAlert();
return;
}
let location = await Location.watchPositionAsync({
accuracy: Location.Accuracy.High,
timeInterval: 5000,
distanceInterval: 50
},
console.log('update location!', location.coords.latitude, location.coords.longitude)
);
setLatitude(location.coords.latitude)
setLongitude(location.coords.longitude);
})();
}, []);
>Solution :
the second parameter of second position suppose to be a callback with the location object as first arg
Location.watchPositionAsync({
accuracy: Location.Accuracy.High,
timeInterval: 5000,
distanceInterval: 50
},
location => {
console.log('update location!', location.coords.latitude, location.coords.longitude)
setLatitude(location.coords.latitude)
setLongitude(location.coords.longitude);
);