【发布时间】:2019-11-16 10:12:20
【问题描述】:
我正在尝试调用:
`navigator.geolocation.requestAuthorization();`
请求地理定位许可。
但是,这会导致在 iOS 模拟器中运行时出错
这在某一时刻工作,但停止了。我试图删除并创建一个新项目。我还尝试卸载/重新安装节点和 react-native-cli。
import React, {Fragment, Component} from 'react';
import {
SafeAreaView,
StyleSheet,
ScrollView,
View,
Text,
StatusBar,
} from 'react-native';
import {
Header,
LearnMoreLinks,
Colors,
DebugInstructions,
ReloadInstructions,
} from 'react-native/Libraries/NewAppScreen';
export default class App extends Component {
constructor(props)
{
super(props);
navigator.geolocation.requestAuthorization();
}
render() {
return (
<View style={styles.MainContainer}>
<SafeAreaView style={{flex: 1, backgroundColor: '#fff'}}>
<Text style={styles.sectionTitle}>Hello World!</Text>
</SafeAreaView>
</View>
);
}
}
const styles = StyleSheet.create({
MainContainer :{
justifyContent: 'center',
flex:1,
margin: 5,
marginTop: (Platform.OS === 'ios') ? 20 : 0,
},
sectionTitle: {
fontSize: 24,
fontWeight: '600',
color: Colors.black,
},
});
我收到此错误:
[error][tid:com.facebook.react.JavaScript] TypeError: undefined is not an object (evaluating 'navigator.geolocation.requestAuthorization')
This error is located at:
in App (at renderApplication.js:40)
in RCTView (at View.js:35)
in View (at AppContainer.js:98)
in RCTView (at View.js:35)
in View (at AppContainer.js:115)
in AppContainer (at renderApplication.js:39)
【问题讨论】:
-
“这在某一时刻工作,但停止了。” newly release version of React Native v0.60 将地理位置移动到separate package,您的项目不再有权访问地理位置库,因此您收到该错误?有潜在错误的日志。您需要提供更多信息。
-
我已成功使用地理定位 API 并能够获取位置。我继续我的项目,添加更多代码,然后开始出现此错误。几天前我刚刚安装了 react-native 堆栈,我没有升级任何东西。我创建了附加的基本代码以在此处发布。
-
您在下面的评论表明您在 0.60。请检查我之前评论中的链接包并安装它如果您使用
react-native init制作项目。如果您没有,请编辑您的问题并包括您如何设置新项目。