我正试图打电话:

`navigator.geolocation.requestAuthorization();`

请求地理定位许可.

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

这在某一点上起作用,但停止了.我试图删除并创建一个新项目.我还try 卸载/重新安装node并对本机cli做出react .

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版本中提取.如果你需要另一种解决方案

安装本地社区/地理位置

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


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

然后

IOS

在使用应用程序时,需要在Info.plist中包含NSLocationWhenInUseUsageDescription键才能启用地理定位.

Android

要请求访问位置,您需要在应用程序的AndroidManifest.xml行中添加以下行

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

usage

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

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

React-native相关问答推荐

try 使用JWT-DECODE解码令牌时出错

Reaction-Native-pdf文件不是PDF格式或已损坏

领带麦克风在Reaction-Native-Incall-Manager中不工作

导出命名空间应首先由 `@babel/plugin-proposal-export-namespace-from` 转换

开发 React Native Apps 时如何在 Android 模拟器窗口中重新加载?

React.js - 为所有组件方法使用属性初始化器

如何在StackNavigator 中实现自定义标题图标?

React-native,iOS模拟器停止响应CMD + D

如何重置react-native 动画?

如何从 React Native 应用程序打开 Google Play store ?

使用 React Navigation 制作离开屏幕的动画

SyntaxError: const 声明中缺少初始化程序

Xcode 设备不可用,未找到运行时配置文件

mapDispatchToProps:有什么意义吗?

React Native + Jest EMFILE:too many open files error

XCode AppIcon 基于方案

使用 React Native 和 Redux 的动画状态管理

在react-native中调试应用程序崩溃

React Native:如何动态更改

保存时 Visual Studio 代码格式化失败