TypeError: undefined is not an object (evaluating 'navigator.geolocation.requestAuthorization')

TypeError: undefined is not an object (evaluating 'navigator.geolocation.requestAuthorization')

我正在尝试呼叫:

`navigator.geolocation.requestAuthorization();`

请求地理定位权限。

但是,当 iOS 模拟器

中 运行 时,这会导致错误

这曾一度有效,但停止了。我试图删除并创建一个新项目。我还尝试 uninstall/reinstall 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,
  },
});

我收到此错误:

[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 版本中提取。如果您需要替代解决方案

安装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 的 'Capabilities' 选项卡中将位置添加为后台模式。

Android

要请求访问位置信息,您需要将以下行添加到应用的 AndroidManifest.xml

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

用法

import Geolocation from '@react-native-community/geolocation';

Geolocation.getCurrentPosition(info => console.log(info));

请确保您已在构建阶段添加 libRCTGeolocation.a --> Link Binary With Libraries 在您的 Xcode 项目设置和 RCTGeolocation.xcodeproj 在您的 Xcode 项目的库下。

从 RN 0.6 或更高版本开始,地理定位不包含在 RN 中 core.If 你想使用它 API,你应该按照这个 link 中的步骤操作:https://github.com/react-native-community/react-native-geolocation

你应该使用@react-native-community/geolocation

首先安装它

yarn add @react-native-community/geolocation

然后导入

import Geolocation from '@react-native-community/geolocation';

在你的函数中

Geolocation.setRNConfiguration(config);

我也在处理同样的问题。后来干脆换了个包就没事了

yarn add react-native-geolocation-service
// import Geolocation from '@react-native-community/geolocation';
import Geolocation from 'react-native-geolocation-service';