Reactjs 由于未定义getCurrentPosition,react本机地理位置无法工作
尝试检索用户的位置时,会显示错误:“无法读取未定义的属性getCurrentPosition”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
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)
仔细研究后,这是正确的解决方案。谢谢