Skip to content

Instantly share code, notes, and snippets.

@antunesleo
Created August 15, 2018 01:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antunesleo/9486b6033d035c2391f38c61964ebdf4 to your computer and use it in GitHub Desktop.
Save antunesleo/9486b6033d035c2391f38c61964ebdf4 to your computer and use it in GitHub Desktop.
Getting location from device after 2 seconds.
import React, { Component } from 'react';
import { View, Text } from 'react-native';
import { Button } from 'react-native';
class GeolocationExample extends Component {
constructor(props) {
super(props);
this.state = {
collecting: false,
latitude: null,
longitude: null,
error: null,
};
}
getLocation(){
let uou = this;
setTimeout(function() {
uou.watchId = navigator.geolocation.watchPosition(
(position) => {
console.log('se liga ai maluco', position);
uou.setState({
latitude: position.coords.latitude,
longitude: position.coords.longitude,
error: null,
});
uou.getLocation();
},
(error) => {
uou.getLocation();
uou.setState({ error: error.message })},
{ enableHighAccuracy: true, timeout: 20000, maximumAge: 2000, distanceFilter: 1 },
);
}, 2000);
}
componentDidMount() {
this.getLocation();
}
componentWillUnmount() {
navigator.geolocation.clearWatch(this.watchId);
}
render() {
return (
<View style={{ flexGrow: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Latitude: {this.state.latitude}</Text>
<Text>Longitude: {this.state.longitude}</Text>
{this.state.error ? <Text>Error: {this.state.error}</Text> : null}
<Button
onPress={this.getLocation.bind(this)}
title="Update location"
color="red"
accessibilityLabel="update location"/>
</View>
);
}
}
export default GeolocationExample;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment