Adding Location Permissions in React Native

Tiempo de lectura: 2 minutos

Reading time: 2 minutes

Good afternoon,

In today’s tutorial, I’m going to show you how to enable location permissions on your device.

Let’s get started

First, we import the API

import * as Location from 'expo-location';

Inside our component, we add the following function that will be responsible for enabling location permissions.

 const [errorMsg, setErrorMsg] = useState(null);

    const permisoLocalizacion = async () => {
        console.log('Obtain: ');
        let { status } = await Location.requestForegroundPermissionsAsync();
        if (status !== 'granted') {
            setErrorMsg('Permission to access location was denied');
            return;
        }
   }

In the button, we call the function to execute that action

<View>
    <Button text={t('refresh_location')} onPress={permisoLocalizacion} />
</View>

Result:

When clicking on Obtain, you will see the location permission being executed

That’s all for today. In the following days, I will show you how to obtain the exact device location

I hope you like it! :slightly_smiling_face:

Leave a Comment