Created
November 28, 2018 04:59
-
-
Save prof3ssorSt3v3/86ead6c99f8f0e5b768adca9260cfe68 to your computer and use it in GitHub Desktop.
This file contains 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 from 'react'; | |
import { StyleSheet, Text, View } from 'react-native'; | |
export default class App extends React.Component { | |
constructor(){ | |
super(); | |
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.geoSuccess, | |
this.geoFailure, | |
geoOptions); | |
} | |
geoSuccess = (position) => { | |
console.log(position.coords.latitude); | |
this.setState({ | |
ready:true, | |
where: {lat: position.coords.latitude,lng:position.coords.longitude } | |
}) | |
} | |
geoFailure = (err) => { | |
this.setState({error: err.message}); | |
} | |
render() { | |
return ( | |
<View style={styles.container}> | |
{ !this.state.ready && ( | |
<Text style={styles.big}>Using Geolocation in React Native.</Text> | |
)} | |
{ this.state.error && ( | |
<Text style={styles.big}>{this.state.error}</Text> | |
)} | |
{ this.state.ready && ( | |
<Text style={styles.big}>{ | |
`Latitude: ${this.state.where.lat} | |
Longitude: ${this.state.where.lng}` | |
}</Text> | |
)} | |
</View> | |
); | |
} | |
} | |
const styles = StyleSheet.create({ | |
container: { | |
flex: 1, | |
backgroundColor: '#fff', | |
alignItems: 'center', | |
justifyContent: 'center' | |
}, | |
big: { | |
fontSize: 48 | |
} | |
}); |
Thanks a lot the code is very clear.
onnum parayanilla
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this thing is so awesome keep educating us more and more from ur yt channel