dig*_*ans 11 geolocation react-native
我正在尝试致电:
`navigator.geolocation.requestAuthorization();`
Run Code Online (Sandbox Code Playgroud)
请求地理位置许可。
但是,这导致在iOS模拟器中运行时出错
此操作在某一时刻起作用,但停止了。我试图删除并创建一个新项目。我也尝试卸载/重新安装node和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,
},
});
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
[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)
Run Code Online (Sandbox Code Playgroud)
sac*_*hew 22
地理位置已从react native .60版本中提取。如果您需要替代解决方案
安装react-native-community / geolocation
npm install @react-native-community/geolocation --save
react-native link @react-native-community/geolocation
Run Code Online (Sandbox Code Playgroud)
然后
iOS
使用应用程序时,您需要输入NSLocationWhenInUseUsageDescription
密钥Info.plist
以启用地理定位。为了在后台启用地理位置,您需要在Info.plist中包括“ NSLocationAlwaysUsageDescription”键,并在Xcode的“功能”标签中将位置添加为背景模式。
安卓系统
要请求访问位置,您需要将以下行添加到您应用的 AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Run Code Online (Sandbox Code Playgroud)
用法
import Geolocation from '@react-native-community/geolocation';
Geolocation.getCurrentPosition(info => console.log(info));
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5109 次 |
最近记录: |