Skip to content

Instantly share code, notes, and snippets.

@prof3ssorSt3v3
Created November 28, 2018 04:59
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save prof3ssorSt3v3/86ead6c99f8f0e5b768adca9260cfe68 to your computer and use it in GitHub Desktop.
Save prof3ssorSt3v3/86ead6c99f8f0e5b768adca9260cfe68 to your computer and use it in GitHub Desktop.
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
}
});
@naseerpervaz
Copy link

Thanks a lot for precise and to the point awesome work

@vamsi920
Copy link

this thing is so awesome keep educating us more and more from ur yt channel

@KiranChimple
Copy link

Thanks a lot the code is very clear.

@rakeshpangil
Copy link

onnum parayanilla

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment