In this tutorial we will learn How To Detect User Location In React Native Android App. Source code to find user location for react native android app is given below.
Table of Contents
React Native Source Code
Simple React Native source code is given below.
{/*install location Dependencies
expo install expo-location */ }
import { StyleSheet, Text, View, } from 'react-native';
import * as Location from 'expo-location';
import React, {useState, useEffect} from 'react';
export default function App() {
const [location , setLocation]= useState(null);
const [errorMsg , setErrorMsg]= useState(null);
useEffect( () => {
(async() => {
let {status}= await Location.requestForegroundPermissionsAsync();
if(status !=='granted')
{
setErrorMsg('Permission to access locatiob was denied');
return;
}
let location= await Location.getCurrentPositionAsync({});
setLocation(location);
})();
},[]);
let text= 'Waiting...';
if(errorMsg)
{
text(errorMsg)
}
else if(location) {
text=JSON.stringify(location);
}
return (
<View style={styles.container}>
<Text> {text} </Text>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
},
});