【问题标题】:TypeError: undefined is not an object (evaluating 'navigator.geolocation.requestAuthorization')TypeError:未定义不是对象(评估“navigator.geolocation.requestAuthorization”)
【发布时间】:2019-11-16 10:12:20
【问题描述】:

我正在尝试调用:

`navigator.geolocation.requestAuthorization();`

请求地理定位许可。

但是,这会导致在 iOS 模拟器中运行时出错

这在某一时刻工作,但停止了。我试图删除并创建一个新项目。我还尝试卸载/重新安装节点和 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)

【问题讨论】:

  • “这在某一时刻工作,但停止了。” newly release version of React Native v0.60 将地理位置移动到separate package,您的项目不再有权访问地理位置库,因此您收到该错误?有潜在错误的日志。您需要提供更多信息。
  • 我已成功使用地理定位 API 并能够获取位置。我继续我的项目,添加更多代码,然后开始出现此错误。几天前我刚刚安装了 react-native 堆栈,我没有升级任何东西。我创建了附加的基本代码以在此处发布。
  • 您在下面的评论表明您在 0.60。请检查我之前评论中的链接包并安装它如果您使用react-native init 制作项目。如果您没有,请编辑您的问题并包括您如何设置新项目。

标签: react-native geolocation


【解决方案1】:

我也在处理同样的问题。后来,我只是简单地换了个包,一切都很好。

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

【讨论】:

    【解决方案2】:

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

    先安装

    yarn add @react-native-community/geolocation
    

    然后导入

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

    在你的函数中

    Geolocation.setRNConfiguration(config);
    

    【讨论】:

      【解决方案3】:

      从 RN 0.6 或更高版本开始,地理定位不包含在 RN 核心中。如果您想使用它的 API,您应该按照以下链接中的步骤操作:https://github.com/react-native-community/react-native-geolocation

      【讨论】:

        【解决方案4】:

        请确保您已在 Build Phases --> Link Binary With Libraries 中添加了 libRCTGeolocation.a 在您的 Xcode 项目设置和 RCTGeolocation.xcodeproj 在您的 Xcode 项目的库下.

        【讨论】:

          【解决方案5】:

          地理位置已从 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 的“功能”选项卡中添加位置作为背景模式。

          安卓

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

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

          用法

          import Geolocation from '@react-native-community/geolocation';
          
          Geolocation.getCurrentPosition(info => console.log(info));
          

          【讨论】:

          • 这并不能解决问题,现在只是给出错误:“未处理的 JS 异常:TypeError: undefined is not an object (evaluating 'navigator.geolocation.getCurrentPosition')”
          • 哪个反应你正在使用的原生版本
          • 如果您不断收到错误消息,请尝试安装 react-native-geolocation-service。链接 - github.com/Agontuk/react-native-geolocation-service
          • react-native-cli: 2.0.1 react-native: 0.60.0 npm: 6.9.0 node: 12.5.0
          • 我找不到任何说geolocation 已被弃用或提取的文档,你能提供任何显示它的链接吗,我只能找到facebook.github.io/react-native/docs/geolocation 没有像你这样说的任何东西说
          猜你喜欢
          • 2021-12-17
          • 2021-12-01
          • 2020-02-09
          • 2019-12-07
          • 2022-01-01
          • 2019-01-20
          • 2014-06-13
          • 2021-07-24
          • 2016-09-02
          相关资源
          最近更新 更多