簡體   English   中英

React-Native Geolocation(Android)錯誤

[英]React-native geolocation (android) error

我試圖在我的本機應用程序上使用地理位置。 我從遵循React Native Geolocation Tutorial開始,但是沒有任何運氣。

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>

    )
  }
}

一段時間后-提示“位置超時結束”消息。 我試圖增加超時時間,但是沒有結果。

GPS,位置,設備上的Wifi已啟用。 添加了Permissios。 Google地圖工作正常。 哪里有問題? 請幫忙!

我有同樣的錯誤,這是由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 }
  );
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM