Created
December 4, 2017 15:14
-
-
Save princessjanf/3bbd30de7f1e9e7a849a74f90953cab5 to your computer and use it in GitHub Desktop.
React Native's geolcoation.getCurrentPosition
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from "react"; | |
import { AppRegistry, StyleSheet, Dimensions, View } from "react-native"; | |
import { TabNavigator } from "react-navigation"; | |
import { Container, Text } from "native-base"; | |
class LocationA extends Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
latitude: null, | |
longitude: null, | |
error:null, | |
}; | |
} | |
componentDidMount() { | |
navigator.geolocation.getCurrentPosition( | |
(position) => { | |
console.log("wokeeey"); | |
console.log(position); | |
this.setState({ | |
latitude: position.coords.latitude, | |
longitude: position.coords.longitude, | |
error: null, | |
}); | |
}, | |
(error) => this.setState({ error: error.message }), | |
{ enableHighAccuracy: false, timeout: 200000, maximumAge: 1000 }, | |
); | |
} | |
render() { | |
return ( | |
<View> | |
<Text> {this.state.latitude} </Text> | |
<Text> {this.state.longitude} </Text> | |
<Text> {this.state.error} </Text> | |
</View> | |
); | |
} | |
} | |
const styles = StyleSheet.create({ | |
container: { | |
position: 'absolute', | |
top: 0, | |
left: 0, | |
right: 0, | |
bottom: 0, | |
justifyContent: 'flex-end', | |
alignItems: 'center', | |
}, | |
map: { | |
position: 'absolute', | |
top: 0, | |
left: 0, | |
right: 0, | |
bottom: 0, | |
}, | |
}); | |
export default LocationA; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment