尝试启用访问iOS设备上的位置服务的权限时出错

时间:2019-07-23 07:45:17

标签: reactjs react-native expo react-native-maps

我正在开发此MapScreen来显示MapView设置为initialRegion的{​​{1}},以获取当前设备用户的坐标。但不幸的是,它无法按预期方式工作,并且未授予权限。

这是我到目前为止尝试实现的:

 export default function MapScreen() {
  const [longitude, setLong] = React.useState();
  const [latitude, setLat] = React.useState();
  const [location, setLocation] = React.useState();
  const [errorMessage, setErrorMessage] = React.useState();

  const _getLocationAsync = async () => {
      let { status } = await Permissions.askAsync(Permissions.LOCATION);
      if (status !== 'granted') {
        setErrorMessage('Permission to access location was denied');
      }

      let location = await Location.getCurrentPositionAsync({});
      let geoLocal = Location.geocodeAsync(location);
      setLocation({ location });
      setLat(geoLocal.latitude);
      setLong(geoLocal.longitude);
    };

  React.useEffect(()=>{
    _getLocationAsync();
  });

  return (latitude && longitude) ? (
      <MapView style={styles.map} initialRegion={{
        latitude,
        longitude,
        latitudeDelta: 1,
        longitudeDelta: 1
      }} />
    ) : null // Displays a blank screen if no long or lat found
 } 

因此我实现的这段代码显示一个空白屏幕,表示未设置经度或纬度并产生错误:[Unhandled promise rejection: Error: LOCATION permission is required to do this operation.]

2 个答案:

答案 0 :(得分:1)

在ios的app.json上添加权限访问权限,并记得在简短的摘要中添加说明。应用想要访问您的相机以捕获用户图像。

"ios": {
      "icon": "./assets/images/iosicon.png",
      "supportsTablet": true,
      "buildNumber": '1.0.0',
      "loadJSInBackgroundExperimental": true,
      "bundleIdentifier": "com.some.app",
      "infoPlist": {
        "NSLocationWhenInUseUsageDescription": "APp would like to access your current location to determine",
        "NSLocationAlwaysUsageDescription": "App would like to access your current location",
        "NSCameraUsageDescription": "App would like to access your Camera"
      }
    },

答案 1 :(得分:0)

当前,条件语句没有任何当前函数。在这种情况下,该功能将在未经许可的情况下执行。

添加elseelse if语句。

  const _getLocationAsync = async () => {
      let { status } = await Permissions.askAsync(Permissions.LOCATION);
      if (status !== 'granted') {
        setErrorMessage('Permission to access location was denied');
      } else if(status == 'granted') {
              let location = await Location.getCurrentPositionAsync({});
              let geoLocal = Location.geocodeAsync(location);
              setLocation({ location });
              setLat(geoLocal.latitude);
              setLong(geoLocal.longitude);
      }
    };