当尝试检索用户的位置时,它显示错误:“无法读取属性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})
}
文档说它不需要导入,所以我看不到我缺少什么?
答案 0 :(得分:0)
npm从react-native-community安装地理位置,然后执行 navigator.geolocation = require('@ react-native-community / geolocation'); 默认情况下,react native> = 0.60
版本中不提供地理位置答案 1 :(得分:0)
我通过以下方法修改了用法,以解决问题
import Geolocation from '@react-native-community/geolocation'
Geolocation.getCurrentPosition(handleSuccess, handleError, geoReqOptions)