2016-08-25 47 views
0

Iam尝试在我的反应原生应用程序上使用地理位置。 我已经开始遵循React Native Geolocation教程,但没有任何运气。反应原生地理位置(Android)错误

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,地点,设备上的Wi-Fi连线Permissios加入谷歌地图做工精细,哪儿有问题请,帮助

回答

0

我同样的错误,这是造成enableHighAccuracy:?!真别知道为什么,但它并没有在所有的工作

+0

我解决了这个问题。我只是去外面,一切正常。 –

0

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

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