权限被拒绝 - 在React Native中使用地理位置定位

10

我一直在尝试使用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',
  },
});

非常感谢您的帮助!

8个回答

20

谢谢!那么模拟器不能获取您的当前位置吗?您必须在调试菜单中自定义一个位置吗? - Peter Qiu

10
在模拟器导航中,选择“调试”,在底部选择“位置”,接下来选择“苹果”,然后按下CMD+R重新加载即可。如图所示:enter image description here

4

如Santosh建议的那样,您需要在iOS模拟器中设置位置:Debug > Location。

请注意,有时即使您在模拟器中设置了位置,仍会出现“PERMISSION_DENIED”错误。 如果发生这种情况,您需要选择不同的预定义位置,然后错误就会消失。


1

我在真实设备上进行测试时出现了相同的错误。我只是重启了设备,然后它开始工作了。任何仍然面临这个问题的人可以尝试一下。


1

补充Santosh的回答,需要注意的是,在我的当前模拟器版本(11.4)中,设置位置的选项可以在以下位置找到:

功能 -> 位置

(由于我还没有被允许发表评论,所以将此作为新答案发布)。


0

所以,对于那些仍在寻找物理设备答案的人

如果你正在使用

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

那么

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

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


0

在我的情况下

我正在使用

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},
);
};

-1
现在是iOS模拟器:功能>位置。在模拟器中添加位置

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接