Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 由于未定义getCurrentPosition,react本机地理位置无法工作_Reactjs_React Native_Geolocation - Fatal编程技术网

Reactjs 由于未定义getCurrentPosition,react本机地理位置无法工作

Reactjs 由于未定义getCurrentPosition,react本机地理位置无法工作,reactjs,react-native,geolocation,Reactjs,React Native,Geolocation,尝试检索用户的位置时,会显示错误:“无法读取未定义的属性getCurrentPosition” import React, {Component} from 'react' import {View, Text, StyleSheet, Image, ScrollView, SafeAreaView} from 'react-native'; import Topnavbar from './TopNavbar' class googleMapsImages extends Component

尝试检索用户的位置时,会显示错误:“无法读取未定义的属性getCurrentPosition”

import React, {Component} from 'react'
import {View, Text, StyleSheet, Image, ScrollView, SafeAreaView} from 'react-native';
import Topnavbar from './TopNavbar'

class googleMapsImages extends Component {
    constructor(props){
        super(props)
        this.state = {
            ready: false, 
            where: {lat: null, lng: null},
            error: null
        }
    }
    componentDidMount(){
        let geoOptions = {
            enableHighAccuracy: true,
            timeOut: 20000,
            maximumAge: 60 * 60 * 24
        }
        this.setState({
            ready: false,
            error: null
        });

        navigator.geolocation.getCurrentPosition( 
            this.geoSucces, 
            this.geoFailure,
            geoOptions)
        }

        geoSucces = (position) => {
            this.setState({ready: true})
        }

        geoFailure = (err) => {
            this.setState({error: err.message})
        }

医生说它不需要导入,所以我看不出我遗漏了什么

npm从react本地社区安装地理定位,然后执行 navigator.geolocation=require(“@react native community/geolocation”);
在react native>=0.60的版本中,默认情况下地理位置不可用。为了解决这个问题,我修改了以下用法

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


Geolocation.getCurrentPosition(handleSuccess, handleError, geoReqOptions)

仔细研究后,这是正确的解决方案。谢谢