TypeError:未定义不是一个对象(评估'navigator.geolocation.requestAuthorization') [英] TypeError: undefined is not an object (evaluating 'navigator.geolocation.requestAuthorization')
问题描述
我正在尝试调用:
`navigator.geolocation.requestAuthorization();`
请求地理定位许可.
但是,这会导致在 iOS 模拟器中运行时出错
But, this is resulting in error when running in iOS simulator
这在某一时刻工作,但停止了.我试图删除并创建一个新项目.我还尝试卸载/重新安装节点和 react-native-cli.
This was working at one point, but stopped. I attempted to delete and create a new project. I also tried to uninstall/reinstall node and 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)
推荐答案
地理位置已从 react native .60 版本中提取.如果您需要其他解决方案
geolocation has been extracted from react native .60 version. If you need an alternative solution
安装 react-native-community/geolocation
install react-native-community/geolocation
npm install @react-native-community/geolocation --save
react-native link @react-native-community/geolocation
然后
IOS
您需要在 Info.plist
中包含 NSLocationWhenInUseUsageDescription
键,以便在使用应用程序时启用地理定位.为了在后台启用地理定位,您需要在 Info.plist 中包含NSLocationAlwaysUsageDescription"键,并在 Xcode 的功能"选项卡中添加位置作为背景模式.
You need to include the NSLocationWhenInUseUsageDescription
key in Info.plist
to enable geolocation when using the app.
In order to enable geolocation in the background, you need to include the 'NSLocationAlwaysUsageDescription' key in Info.plist and add location as a background mode in the 'Capabilities' tab in Xcode.
安卓
要请求访问位置,您需要将以下行添加到应用的 AndroidManifest.xml
To request access to location, you need to add the following line to your app's AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
用法
import Geolocation from '@react-native-community/geolocation';
Geolocation.getCurrentPosition(info => console.log(info));
这篇关于TypeError:未定义不是一个对象(评估'navigator.geolocation.requestAuthorization')的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!