Permission denied -React Native中的地理位置

xmd2e60i  于 2023-08-07  发布在  React
关注(0)|答案(8)|浏览(121)

我一直在玩React Native,让自定义位置工作并设置“NSLocationWhenInUseUsageDescription”键。在iOS模拟器上运行时,错误是这样的:

{
 "code": 2,
 "message": "Unable to retrieve location.",
 "PERMISSION_DENIED": 1,
 "POSITION_UNAVAILABLE": 2,
 "TIMEOUT": 3 
}

字符串
这就是我所拥有的,几乎直接来自地理位置示例页面https://facebook.github.io/react-native/docs/geolocation.html

/* eslint no-console: 0 */
'use strict';

var React = require('react');
var ReactNative = require('react-native');
var {
  StyleSheet,
  Text,
  View,
} = ReactNative;

export default class GeolocationExample extends React.Component {
  state = {
    initialPosition: 'unknown'
  };

  componentDidMount() {
    navigator.geolocation.getCurrentPosition(
      (position) => {
        var initialPosition = JSON.stringify(position);
        this.setState({initialPosition});
      },
      (error) => alert(JSON.stringify(error)),
      {enableHighAccuracy: true, timeout: 20000, maximumAge: 1000}
    );
  }

  render() {
    return (
      <View>
        <Text>
          <Text style={styles.title}>Initial position: </Text>
          {this.state.initialPosition}
        </Text>
      </View>
    );
  }
}

var styles = StyleSheet.create({
  title: {
    fontWeight: '500',
  },
});


任何帮助将不胜感激!

lymnna71

lymnna711#

您需要在模拟器中设置位置。在模拟器菜单中有一个选项,如本问题所述:Set the location in iPhone Simulator

bfrts1fy

bfrts1fy2#

在模拟器导航器中,选择调试,在底部选择位置,然后选择苹果,然后CMD+R重新加载,它工作了。

的数据

hsvhsicv

hsvhsicv3#

正如Santosh所建议的,您需要在iOS模拟器中设置位置:调试>位置。
请记住,有时,你会得到“PERMISSION_DENIED”错误,即使你在模拟器中设置了位置。如果发生这种情况,您需要选择一个不同的预定义位置,然后错误就会消失。

hlswsv35

hlswsv354#

在Santosh的答案中添加更新,请注意,在我当前版本的模拟器(11.4)中,可以在以下位置找到设置位置的选项:
特点->位置
(这是一个新的答案,因为我还没有评论)。

eqfvzcg8

eqfvzcg85#

我在一个真实的设备上测试,它给出了同样的错误。我只是重新启动我的设备,它开始工作。任何仍然面临这个问题的人都可以给予一下。

mepcadol

mepcadol6#

所以,对于那些仍在寻找答案的物理设备
如果你正在使用

import Geolocation from '@react-native-community/geolocation';

字符串
然后,

Geolocation.getCurrentPosition(
                info => {console.log(info)},error=>{console.log(error)}});


如果你不处理错误,它会抛出错误或使你的应用程序崩溃。

9q78igpj

9q78igpj7#

在我的情况下
我在用

import Geolocation from 'react-native-geolocation-service';

字符串
所以就用了

const requestCameraPermission = async () => {
if (Platform.OS === 'android') {
  try {
    const granted = await PermissionsAndroid.request(
      PermissionsAndroid.PERMISSIONS.CAMERA,
      {
        title: `Allow Yarrow to access this devices's location.`,
        message: '',
        buttonNeutral: 'Ask Me Later',
        buttonNegative: 'Cancel',
        buttonPositive: 'OK',
      },
    );
    if (granted === PermissionsAndroid.RESULTS.GRANTED) {
      console.log('You can use the location');
    } else {
      console.log('location permission denied');
    }
  } catch (err) {
    console.warn(err);
  }
} else {
  const auth = await Geolocation.requestAuthorization('whenInUse');
  setIosLocation(auth);
  console.log(auth);
}
};

 useEffect(() => {
   requestCameraPermission();
 }, []);
 
  const locationGet = async () => {
setLoading(true);
if (Platform.OS === 'ios') {
  if (iosLocation !== 'granted') {
    return Alert.alert(
      'Alert!',
      `We're unable to connect to your location. Please provide the location access.`,
      [{text: 'Ok'}],
      {cancelable: true},
    );
  }
}

Geolocation.getCurrentPosition(
  position => {
    console.log(position);
    setTimeout(() => {
      setLoading(false);
    }, 1000);
  },
  error => {
    // See error code charts below.
    console.log('error in location :->> ', error.code, error.message);
  },
  {enableHighAccuracy: true, timeout: 15000, maximumAge: 10000},
);
};

esyap4oy

esyap4oy8#

iOS Simulator:功能> Location.

相关问题