【问题标题】:Error trying to enable permission to access Location services on iOS device尝试在 iOS 设备上启用访问定位服务的权限时出错
【发布时间】:2019-07-23 07:45:17
【问题描述】:

我正在开发这个MapScreen 以显示MapView 并设置initialRegion 以获取设备用户的坐标。但不幸的是,它没有按预期工作并且没有授予权限。

这是我目前尝试实现的:

 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.]

【问题讨论】:

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


    【解决方案1】:

    在 app.json 上为 ios 添加权限访问,并记得在简短的 ex 中添加描述。应用想要访问您的相机以捕捉用户图像。

    "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"
          }
        },
    

    【讨论】:

      【解决方案2】:

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

      添加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);
            }
          };
      

      【讨论】:

      • 如果您接受许可,您可能不会在条件声明中输入接受。尝试刷新。
      • 刷新的时候不请求权限就成功了。
      • 接受声明或提议未出现在我的设备上
      猜你喜欢
      • 1970-01-01
      • 2016-05-05
      • 1970-01-01
      • 2017-04-19
      • 1970-01-01
      • 1970-01-01
      • 2016-01-26
      • 1970-01-01
      • 2012-11-27
      相关资源
      最近更新 更多