React Native Geolocation Coordinates

React Native is using web base Geolocation API to get the position of the device. If you go through the Geolocation API documentation then you will find various methods like getCurrentPosition(), watchPosition() which are available on the React Native too.

Let’s Started

We are going to use expo to create and setup the demo. We don’t require Android Studio or XCode for simulator if we are using expo. Make sure you have installed expo-cli on your system. So let’s started to creating a GeolocationDemo project.

expo init geolocationDemo

Above command will create a directory on you current location with geolocationDemo name. Once initialization process finish, go to the geolocationDemo directory and run the following command.

npm start

You can also run expo start command to starting your project.

How to access Geolocation

Geolocation API’s getCurrentPosition() method accepts 3 parameters. First parameter is required, which is a callback method and will trigger if everything works perfectly. The other 2 parameters are optional. Lets me show you the code for better understand.

            position => {
                const location = JSON.stringify(position);
            error => {
                // handle error
                enableHighAccuracy: true, 
                timeout: 20000, 
                maximumAge: 1000 

As you can see on the above code, I have used all the 3 parameters on getCurrentPosition() method. The first parameter will trigger once we get the position successfully. The second parameter will trigger if we get any error to get the positions, basically, the 2nd parameter is for error handling. And the 3rd parameter is for options.

The success callback will return the following position data in JSON format.


Implement Geolocation API with React Native

Let start implementing the Geolocation API with React Native. First, open the App.js in your favorite editor and apply the following codes.

import React from 'react';
import { 
} from 'react-native';

class App extends React.Component {

    constructor(props) {

        this.state = {
            location: null, 
            underProcess: false

    getCoordinates = () => {

        this.setState({underProcess: true, location: null});

            position => {
                const location = JSON.stringify(position);
                this.setState({underProcess: false, location: location});
            error => {
                this.setState({underProcess: false});
            { enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 }

    render() {
        return (
            <View style={styles.container}>
                { this.state.location !== null &&
                    <Text>Location: {this.state.location}</Text>
                { this.state.underProcess &&
                    title="Get Location"

const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: '#fff',
        alignItems: 'center',
        justifyContent: 'center',

export default App;

The above code will generate the following output on your phone with your Geolocation.