1

我在我的 Android 设备上启用了定位服务,但我不断收到上述错误。它在加载 Expo(第一次)时提示我的权限请求,但我仍然收到承诺拒绝。它曾经工作正常,但突然停止工作。下面是我请求权限并执行位置的代码。

注意:这在 iOS 上运行良好,并且在托管工作流程中。

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
        }

        },
      [],
        )
    )
4

1 回答 1

0

调用 catch 块中的函数,直到获得坐标。

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();
          }
        }
于 2021-09-30T10:27:23.710 回答