TypeError:未定义不是对象(正在评估“ navigator.geolocation.requestAuthorization”)

时间:2019-07-05 20:12:23

标签: react-native geolocation

我正尝试致电:

`navigator.geolocation.requestAuthorization();`

请求地理位置许可。

但是,这导致在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,
  },
});

我收到此错误:

[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)

5 个答案:

答案 0 :(得分:0)

地理位置已从react native .60版本中提取。如果您需要替代解决方案

安装react-native-community /地理位置

npm install @react-native-community/geolocation --save


react-native link @react-native-community/geolocation

然后

IOS

您需要在NSLocationWhenInUseUsageDescription中包含Info.plist键,才能在使用该应用程序时启用地理位置。 为了在后台启用地理位置,您需要在Info.plist中包含“ NSLocationAlwaysUsageDescription”键,并在Xcode的“功能”标签中将位置添加为背景模式。

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));

答案 1 :(得分:0)

请确保已在构建阶段-> Xcode项目设置上的库链接二进制文件和Xcode项目库下的 RCTGeolocation.xcodeproj 中添加了 libRCTGeolocation.a

答案 2 :(得分:0)

从RN 0.6或更高版本开始,RN核心中不包括地理位置。如果要使用其API,则应遵循以下链接中的步骤:https://github.com/react-native-community/react-native-geolocation

答案 3 :(得分:0)

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

首先安装

yarn add @react-native-community/geolocation

然后导入

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

在您的功能中

Geolocation.setRNConfiguration(config);

答案 4 :(得分:0)

我也在处理同样的问题。后来,我简单地改变了包装,一切都很好。

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