Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Updating users location every x amount of seconds

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/

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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);
      );
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading