Created
December 27, 2019 18:34
-
-
Save fabiobarboza7/4074a5c36b168a8001873ee9a293a662 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, { useEffect, useState } from 'react'; | |
import { View, StyleSheet, Text, PermissionsAndroid } from 'react-native'; | |
import Geolocation from 'react-native-geolocation-service'; | |
const styles = StyleSheet.create({ | |
container: { | |
flex: 1, | |
backgroundColor: 'lightgreen', | |
}, | |
text: { | |
color: 'orange', | |
fontSize: 40, | |
}, | |
}); | |
export default function App() { | |
const [hasLocationPermission, setHasLocationPermission] = useState(false); | |
const [userPosition, setUserPosition] = useState(false); | |
async function verifyLocationPermission() { | |
try { | |
const granted = await PermissionsAndroid.request( | |
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION | |
); | |
if (granted === PermissionsAndroid.RESULTS.GRANTED) { | |
console.log('permissão concedida'); | |
setHasLocationPermission(true); | |
} else { | |
console.error('permissão negada'); | |
setHasLocationPermission(false); | |
} | |
} catch (err) { | |
console.warn(err); | |
} | |
} | |
useEffect(() => { | |
verifyLocationPermission(); | |
if (hasLocationPermission) { | |
Geolocation.getCurrentPosition( | |
position => { | |
setUserPosition({ | |
latitude: position.coords.latitude, | |
longitude: position.coords.longitude, | |
}); | |
}, | |
error => { | |
console.log(error.code, error.message); | |
} | |
); | |
} | |
}, [hasLocationPermission]); | |
return ( | |
<View style={styles.container}> | |
<Text>Latitude: {userPosition.latitude}</Text> | |
<Text>Longitude: {userPosition.longitude}</Text> | |
</View> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment