【问题标题】:location permission not granted in react native在本机反应中未授予位置权限
【发布时间】:2020-01-24 07:45:06
【问题描述】:

我在这里找到位置的小问题是我的代码

export default class  App extends React.Component{
  state = {
        location: null
    };

    findCoordinates = () => {
        Geolocation.getCurrentPosition(
            position => {
                const location = JSON.stringify(position);
                this.setState({ location });
            },
      error => Alert.alert(error.message),
      { enableHighAccuracy: true, timeout: 15000, maximumAge: 10000 }
    );
    };
  render(){
    return (
      <View style={styles.container}>
                <TouchableOpacity onPress={this.findCoordinates}>
                    <Text style={styles.welcome}>Find My Coords?</Text>
                    <Text>Location: {this.state.location}</Text>
                </TouchableOpacity>
            </View>
    );
  }
}

他们显示未授予位置权限

【问题讨论】:

    标签: react-native react-native-android


    【解决方案1】:

    首先获取用户的位置权限。使用这个功能

    import { Platform, PermissionsAndroid } from 'react-native';
    
    import Geolocation from 'react-native-geolocation-service';
    
    async function requestPermissions() {
      if (Platform.OS === 'ios') {
        Geolocation.requestAuthorization();
        Geolocation.setRNConfiguration({
          skipPermissionRequests: false,
         authorizationLevel: 'whenInUse',
       });
      }
    
      if (Platform.OS === 'android') {
        await PermissionsAndroid.request(
          PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
        );
      }
    }
    

    【讨论】:

    • 我不知道必须获得用户的许可。所以请告诉必须获得许可
    • 如果我关闭应用程序,甚至请求用户的许可。我将如何获得用户的许可
    【解决方案2】:

    上述答案不再有效,因为 setRNConfiguration 函数现已弃用。

    这是获取用户使用位置权限的更新方式:

    import { Platform, PermissionsAndroid } from 'react-native';
    import Geolocation from 'react-native-geolocation-service';
    
    async function requestPermissions() {
      if (Platform.OS === 'ios') {
        const auth = await Geolocation.requestAuthorization("whenInUse");
        if(auth === "granted") {
           // do something if granted...
        }
      }
    
      if (Platform.OS === 'android') {
        await PermissionsAndroid.request(
          PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
        );
        if (granted === PermissionsAndroid.RESULTS.GRANTED) {
          // do something if granted...
        }
      }
    }
    

    【讨论】:

    • 你能告诉我这个“授予”在哪里吗 if (granted === PermissionsAndroid.RESULTS.GRANTED) {}
    【解决方案3】:

    请这样做

    async function requestPermissions() {
    if (Platform.OS === 'ios') {
      const auth = await Geolocation.requestAuthorization('whenInUse');
      if (auth === 'granted') {
        _getCourts();
      }
    }
    
    if (Platform.OS === 'android') {
      const granted = await PermissionsAndroid.request(
        PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
      );
      if (granted === PermissionsAndroid.RESULTS.GRANTED) {
        _getCourts();
      }
    }
    

    }

    【讨论】:

      【解决方案4】:

      UsmanJ 是正确的,但有一个错误。 这是修复:

      更改此行:if (granted === PermissionsAndroid.RESULTS.GRANTED) {

      收件人:if ("granted" === PermissionsAndroid.RESULTS.GRANTED) {

      【讨论】:

        猜你喜欢
        • 2017-05-30
        • 1970-01-01
        • 1970-01-01
        • 2022-06-28
        • 2021-05-02
        • 2022-08-24
        • 1970-01-01
        • 2016-03-08
        相关资源
        最近更新 更多