【问题标题】:react native geolocation does not work because of undefined getCurrentPosition由于未定义的getCurrentPosition,反应本机地理位置不起作用
【发布时间】:2019-10-28 09:55:24
【问题描述】:

当试图检索用户的位置时,它显示一个错误:'无法读取属性 getCurrentPosition' of undefined。

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

文档说它不需要导入,所以我看不出我在哪里遗漏了什么?

【问题讨论】:

    标签: reactjs react-native geolocation


    【解决方案1】:

    npm install geolocation from react-native-community 然后执行 navigator.geolocation = require('@react-native-community/geolocation'); 在 react native >=0.60

    的版本中,地理位置默认不可用

    【讨论】:

    • 查看后,这是正确的解决方案。谢谢!
    【解决方案2】:

    我使用以下方法修改了我的用法以解决此问题

    import Geolocation from '@react-native-community/geolocation'
    
    
    Geolocation.getCurrentPosition(handleSuccess, handleError, geoReqOptions)
    

    【讨论】:

      猜你喜欢
      • 2017-11-19
      • 2013-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-03
      • 2018-03-26
      • 1970-01-01
      相关资源
      最近更新 更多