原生地理定位(android)错误



我试图在我的react-native应用程序上使用地理定位。我已经开始遵循React Native地理定位教程,但我没有任何运气。

import React, {Component} from 'react'
import {View, Text, TouchableOpacity} from 'react-native'
import {styles} from './locationPageStyles.js'
export default class LocationPage extends Component {

  constructor(props) {
    super(props)
    this.state = {
      initialPosition: 'unknownn',
      lastPosition: 'unknown'
    }
  }
  componentDidMount() {
   navigator.geolocation.getCurrentPosition(
     (position) => {
       var initialPosition = JSON.stringify(position);
       this.setState({initialPosition});
     },
     (error) => alert(error),
     {enableHighAccuracy: true, timeout: 20000, maximumAge: 1000}
    );
    this.watchID = navigator.geolocation.watchPosition((position) => {
      var lastPosition = JSON.stringify(position);
      this.setState({lastPosition});
    });
  }
 componentWillUnmount() {
   navigator.geolocation.clearWatch(this.watchID);
 }
  render() {
    return(
      <View style={styles.container}>
        <TouchableOpacity onPress={() => this.props.navigator.push({component: "startPage"})} >
          <View style={{paddingVertical: 10, paddingHorizontal: 20, backgroundColor: 'yellow'}}>
            <Text>Go to page ONE</Text>
          </View>
        </TouchableOpacity>
        <View>
         <Text>There is your geo {this.state.initialPosition} and {this.state.lastPosition}</Text>
        </View>
      </View>
    )
  }
}

经过一段时间后-提示"位置超时结束"消息。我试图增加timeOut,但没有结果。(

GPS,位置,Wifi在设备上启用。许可补充道。谷歌地图工作得很好。问题在哪里?请帮助!

我有同样的错误,这是由enableHighAccuracy: true引起的。不知道为什么,用了它,它就根本不起作用了。

使用这个代码,它可能会帮助你

 - Don't forget to give location permission in manifest file

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

constructor(props){
    super(props);
    this.state = {
        latitude: null,
        longitude: null,
        error: null,
      };
}
componentDidMount(){
    navigator.geolocation.getCurrentPosition( (position)=>{
        //const ipos = JSON.stringify(Position);
        this.setState({
            latitude: position.coords.latitude,
            longitude: position.coords.longitude,
            error: null,
          });
    },
    (error) => this.setState({ error: error.message }),
    { enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 }
  );
}

最新更新