Created
August 15, 2018 01:35
-
-
Save antunesleo/9486b6033d035c2391f38c61964ebdf4 to your computer and use it in GitHub Desktop.
Getting location from device after 2 seconds.
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 { 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