# Calculate Distance Between Two Locations in React Native App

In this post, we will see how to calculate the distance between two locations in the React Native app. You will see basic distance calculation operation. I have used Geolib to get the distance between two lat-long. This library is very simple and provides a number of functions to perform different geospatial operations like distance calculation, conversion of decimal coordinates to sexagesimal and vice versa, etc. Currently, it supports 2D only which means it does not support altitude/elevation for now.

From the number of functions, we are going to use two functions for the distance calculation actually both calculate the distance but one provides normal distance and the other one provides precise distance. Have a look at the two points below:

## 1. Normal Distance Calculation

Normal distance calculation can be done using getDistance() function which calculates the distance between two geo coordinates.

This function takes up to 3 arguments. First 2 arguments are mandatory. Coordinates can be in sexagesimal(“51° 31′ N”) or decimal(51.5103) format. The third argument is accuracy (in meters). By default, the accuracy is 1 meter. If you need a more accurate result, you can set it to a lower value, e.g. to 0.01 for centimeter accuracy.

## 2. Precise Distance Calculation

Precise distance calculation can be done using getPreciseDistance() function which calculates the distance between two geo coordinates. This method is more accurate then getDistance, especially for long distances but it is also slower. It is using the Vincenty inverse formula for ellipsoids.

The argument description is the same as getDistance() method above.

So that were the two functions which we are going to use.

In this example, we are going to make a single screen contains two buttons to show an alert when clicked. One will show the normal distance and the other one will show the precise distance so let’s get started.

## To Make a React Native App

Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react-native init to make our React Native App. Assuming that you have node installed, you can use npm to install the `react-native-cli` command line utility. Open the terminal and go to the workspace and run

``npm install -g react-native-cli``

Run the following commands to create a new React Native project

``react-native init ProjectName``

If you want to start a new project with a specific React Native version, you can use the --version argument:

``react-native init ProjectName --version X.XX.X``
``react-native init ProjectName --version react-native@next``

This will make a project structure with an index file named App.js in your project directory.

## Installation of Dependency

To use `getDistance` and `getPreciseDistance` functions we have to install `geolib` dependency. To install the dependency open the terminal and jump into your project

Now install the dependency

## Code to Calculate Distance Between Two Locations

Now Open App.js in any code editor and replace the code with the following code.

## To Run the React Native App

Open the terminal again and jump into your project using.
``cd ProjectName``
To run the project on an Android Virtual Device or on real debugging device
``react-native run-android``
or on the iOS Simulator by running
``react-native run-ios` (macOS only).`

This is how you can calculate the distance between two locations in the React Native app. If you have any doubts or you want to share something about the topic you can comment below or contact us here. There will be more posts coming soon. Stay tuned!

Hope you liked it. 🙂

### 6 thoughts on “Calculate Distance Between Two Locations in React Native App”

1. thank you

2. But it’s not showing the accurate distance

• Can you please share more details?

3. var dis = getDistance(
{ latitude: this.state.lat, longitude: this.state.long },
{ latitude: this.state.cordLatitude, longitude: this.state.cordLongitude }
);
alert(`Distance\n\${dis} Meter\nor\n\${dis / 1000} KM`);
var di =getPreciseDistance(
{ latitude: this.state.lat, longitude: this.state.long },
{ latitude: this.state.cordLatitude, longitude: this.state.cordLongitude }
);
alert(`Distance\n\${di} Meter\nor\n\${di / 1000} KM`);
}

• ??

4. I have start and end location which is current location i want to find the distance between start and end location

This site uses Akismet to reduce spam. Learn how your comment data is processed.