【问题标题】:React Native - unable to get current locationReact Native - 无法获取当前位置
【发布时间】:2021-12-30 17:07:49
【问题描述】:

当用户选中“获取当前位置”复选框时,我想获取当前位置。

我有一个用于用户输入的表单,其中之一是一个复选框,当复选框被选中并提交表单时,它会获取用户的当前位置。

我的代码:

const getLocation = () => {
console.log("Testing getLocation");
(async () => {
  console.log("Ask Location Permission");
  let { status } = await Location.requestForegroundPermissionsAsync();
  console.log("Permission Asked");
  if (status !== "granted") {
    setErrorMsg("Location access denied!");
    console.log(errorMsg);
    return;
  }
  let currentLocation = await Location.getCurrentPositionAsync({});
  setLatitude(currentLocation.coords.latitude);
  setLongitude(currentLocation.coords.longitude);
  console.log("Start of Print Coords");
  console.log(currentLatitude);
  console.log(currentLongitude);
  console.log("End of Print Coords");
})();
};

const onSubmit = (data: { distance: number; coordinates: string }) => {
if (checkbox) {
  console.log("Checkbox Selected");
  getLocation();
  console.log("getLocation Tested");
  coords.push(currentLatitude);
  coords.push(currentLongitude);
  console.log(coords);
}

当我提交表单时,我有多个控制台日志来检查我的问题所在,因为它从不要求获取位置权限。在 getLocation 中,它在打印“Ask Location Permission”后退出,并在应用程序未请求位置许可时以某种方式返回打印“Permission Asked”。console logs

我正在使用 expo location 来获取当前位置,并且我不使用 useEffect,因为我只需要一次当前坐标并且不需要更新它。 https://docs.expo.dev/versions/latest/sdk/location/

【问题讨论】:

    标签: react-native expo location


    【解决方案1】:

    在您的情况下,异步函数仅在 getLocation() 中声明。您需要像下面这样调用该函数。

    const getLocation = () => {
    console.log("Testing getLocation");
    async () => {
      console.log("Ask Location Permission");
      let { status } = await Location.requestForegroundPermissionsAsync();
      if (status !== "granted") {
        setErrorMsg("Location access denied!");
        console.log(errorMsg);
        return;
      }
      let currentLocation = await Location.getCurrentPositionAsync({});
      setLatitude(currentLocation.coords.latitude);
      setLongitude(currentLocation.coords.longitude);
      console.log(currentLatitude);
      console.log(currentLongitude);
    }(); // here you need to call it using parenthesis
    };
    

    【讨论】:

    • 添加额外的括号有助于打印“询问位置权限”但是,它仍然不要求位置权限螺母,它会打印出“询问权限”。我已经更新了现在有更多控制台日志的代码。
    • 尝试使用try catch 块来查看哪个异步调用引发了错误。我认为您需要添加权限。在此处查看世博会权限指南docs.expo.dev/guides/permissions
    • 我添加了一个try catch,它仍然绕过了权限请求。不知道发生了什么
    【解决方案2】:

    有一种情况,以前用户选择不再请求此应用程序的权限。下次应用请求此权限时,设备会静默返回status: "denied" 无权限弹出模式。

    始终检查应用是否有资格再次请求该权限。

      let { status,canAskAgain } = await Location.requestForegroundPermissionsAsync();
      console.log("Permission Asked");
    
    
    if(canAskAgain === false){
    
    // User has previously blocked app to ask this permission
    // Force user to grant permission manually
    
     await Linking.openSettings();
    
    }
      if (status !== "granted") {
        setErrorMsg("Location access denied!");
        console.log(errorMsg);
        return;
      }
    
    

    【讨论】:

    • 我已经尝试过了,但它仍然不要求位置许可。我已经检查了设置和位置权限。
    • 这看起来问题出在 Expo Permission Implementation 上。 Expo 存储库github.com/expo/expo/issues/15273# 存在持续问题
    • 如果我使用他们文档中的代码(如果它不在函数docs.expo.dev/versions/latest/sdk/location 中),则获取位置有效。虽然在函数中使用他们的代码会给我一个错误
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-12
    • 2019-03-13
    • 2018-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多