React native TypeError:undefined不是对象(正在评估';navigator.geolocation.requestAuthorization';)
我正试图打电话:React native TypeError:undefined不是对象(正在评估';navigator.geolocation.requestAuthorization';),react-native,geolocation,React Native,Geolocation,我正试图打电话: `navigator.geolocation.requestAuthorization();` 请求地理定位许可 但是,这会导致在iOS模拟器中运行时出错 这是一个工作点,但停止。我试图删除并创建一个新项目。我还尝试卸载/重新安装node并对本机cli作出反应 import React, {Fragment, Component} from 'react'; import { SafeAreaView, StyleSheet, ScrollView, View
`navigator.geolocation.requestAuthorization();`
请求地理定位许可
但是,这会导致在iOS模拟器中运行时出错
这是一个工作点,但停止。我试图删除并创建一个新项目。我还尝试卸载/重新安装node并对本机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,
},
});
地理位置已从react native.60版本中提取。如果你需要一个替代方案 安装本地社区/地理位置
npm install @react-native-community/geolocation --save
react-native link @react-native-community/geolocation
然后
IOS
您需要在Info.plist
中包含nslocation whenUsageDescription
键,以便在使用应用程序时启用地理定位。
为了在后台启用地理位置,您需要在Info.plist中包含'NSLocationAlwaysUsageDescription'键,并在Xcode的'Capabilities'选项卡中添加位置作为后台模式
Android
要请求访问位置,您需要在应用程序的AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
请确保您已添加了libRCTGeolocation.a在构建阶段-->链接二进制文件与Xcode项目设置中的库,以及Xcode项目库下的RCTGeolocation.xcodeproj。从RN 0.6或更高版本开始,RN core中不包括地理位置。如果您想使用其API,您应该按照此链接中的步骤操作:您应该使用@react native community/geolocation
npm install @react-native-community/geolocation --save
react-native link @react-native-community/geolocation
首先安装它
yarn add @react-native-community/geolocation
然后导入
import Geolocation from '@react-native-community/geolocation';
在你的职责范围内
Geolocation.setRNConfiguration(config);
“这在某一点上起作用,但停止了。”我成功地使用了地理定位api并能够获得位置。我继续我的项目,添加更多的代码,然后开始出现这个错误。几天前我刚刚安装了react本机堆栈,我没有升级任何东西。我创建了附加的裸体代码发布在这里。你下面的评论表明你在0.60上。请检查我之前评论中的链接包,如果您使用
react native init
创建项目,请安装它。如果没有,请编辑您的问题并包括如何设置新项目。这并不能解决问题,现在只会给出错误:“未处理的JS异常:TypeError:undefined不是对象(评估‘navigator.geolocation.getCurrentPosition’)”您使用的是哪个react本机版本如果一直出现错误,请尝试安装react本机地理位置服务。link-react native cli:2.0.1 react native:0.60.0 npm:6.9.0 node:12.5.0我找不到任何说明geolocation
已被弃用或提取的文档,你能提供任何显示该文档的链接吗,我只能找到没有像你所说的任何内容的文档
Geolocation.setRNConfiguration(config);