TypeError:未定义不是对象(正在评估"navigator.geolocation.requestAuthorization") [英] TypeError: undefined is not an object (evaluating 'navigator.geolocation.requestAuthorization')

查看:146
本文介绍了TypeError:未定义不是对象(正在评估"navigator.geolocation.requestAuthorization")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试致电:

`navigator.geolocation.requestAuthorization();`

请求地理位置许可.

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

But, this is resulting in error when running in iOS simulator

这是工作的一点,但停止了.我试图删除并创建一个新项目.我还尝试卸载/重新安装node和react-native-cli.

This was working at one point, but stopped. I attempted to delete and create a new project. I also tried to uninstall/reinstall node and 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版本中提取.如果您需要替代解决方案

geolocation has been extracted from react native .60 version. If you need an alternative solution

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

install 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的功能"标签中将位置添加为背景模式.

You need to include the NSLocationWhenInUseUsageDescription key in Info.plist to enable geolocation when using the app. In order to enable geolocation in the background, you need to include the 'NSLocationAlwaysUsageDescription' key in Info.plist and add location as a background mode in the 'Capabilities' tab in Xcode.

Android

要请求访问位置,您需要在应用程序的AndroidManifest.xml

To request access to location, you need to add the following line to your app's AndroidManifest.xml

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

用法

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

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

这篇关于TypeError:未定义不是对象(正在评估"navigator.geolocation.requestAuthorization")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆