权限被拒绝 – React Native中的地理位置

我一直在玩React Native,获取自定义位置并设置“NSLocationWhenInUseUsageDescription”键。 在ios模拟器上运行时,错误是:

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

这就是我所拥有的,几乎直接来自Geolocation示例页面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 (   Initial position:  {this.state.initialPosition}   ); } } var styles = StyleSheet.create({ title: { fontWeight: '500', }, }); 

任何帮助,将不胜感激!

您需要在模拟器中设置位置。 如此问题中所述,在模拟器菜单中有一个选项: 在iPhone模拟器中设置位置

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

请记住,即使您在模拟器中设置了位置,您也会不时得到“PERMISSION_DENIED”错误。 如果发生这种情况,您需要选择不同的预定义位置,然后错误消失。