I have enable location services on my Android device, but I keep getting the above error. It prompts me for my permissions request upon loading Expo (for the first time) but I still get the promise rejection. It used to work fine, but all of a sudden, stopped working. Below is my code requesting the permissions, and executing the location.

Note: This works fine on iOS, and this is in managed workflow.

useFocusEffect( 
      React.useCallback(()=> {

      let isActive = true;

        async function getLocationAsync() {
              let { status } = await Location.requestForegroundPermissionsAsync()
                  if (status !== 'granted'){
                      setErrorMsg('Permission to access location was denied')
                      return;      
                  } 
                    
                let location = await Location.getCurrentPositionAsync({});
                setLocation(location);
                console.log('Location permissions granted')
        }
        console.log(location)
  
          getLocationAsync()
          console.log(Permissions.LOCATION)
          console.log('Location status above')
      
        return () =>{
          isActive = false
        }

        },
      [],
        )
    )
1

There are 1 answers

1
Carlos Martinez Ival On

Call the function in the catch block until you get the coordinates.

getLocationAsync = async () => {
        let { status } = await Location.requestForegroundPermissionsAsync();
        if (status !== 'granted') {
          this.setState({
            errorMessage: 'Permission to access location was denied',
          });
          console.log('Permission to access location was denied')
        }else{
          try{
          let location = await Location.getCurrentPositionAsync({ enableHighAccuracy: true });
          
          const { latitude, longitude } = location.coords
           });
          }catch(error){
            // Call the function until get the coordinates
            this.getLocationAsync();
          }
        }