# Calculate the distance between two points on Earth using Javascript

# The distance on a plane

Given the X and Y position on a flat plane we can calculate the distance between two points using the Pythagorean theorem.

Which visualised as an equation looks like this:

Let’s turn this into some code:

Firstly, javascript provides us with a Math object, which is a core utility library.

Let’s start with the inner most part and work our way out:

What we actually need here is a difference calculation, which as we know will be the absolute value of the result. e.g:

The difference between 5 and 20 is 15, we can calculate this with

`abs(20–5) = 15 or abs(5-20) = 15`

Next we need to square the result with the Math.pow function:

`Math.pow(result, 2);`

Then we need to **add** the x and y resulting equations and **square root** the result using the Math.sqrt. The entire equation in some pseudo Javascript would look at follows:

`Math.sqrt(Math.pow(Math.abs(x1 - x2)) + Math.pow(Math.abs(y1 - y2)))`

This is straight forward enough on a flat plane, however, this becomes quite inaccurate when plotting this on the earth which is an essentially a sphere (oblate spheroid).

# The distance on a sphere

In order to do this more accurately we need **two** additional equations.

First, we need a way to calculate the angle between the two points from the centre of the sphere, followed by an equation to calculate the distance between two points given the central angle (the result of the former equation). Fortunately we have mathematical formulas to do both.

Let’s begin with calculating the angle between two points from the centre (also known as the central subtended angle).

**Δ (delta)**representing the**σ**being the central subtended angle and the result we are looking for**arcos**or the inverse cosine (cos to the power of -1) is represented by**Φ**representing the latitude values and**λ**the longitude.

Lets brake this equation down into some pseudo code as we did before:

Before we begin, we realise that we need to deal with radians as opposed to degrees, as this is a far more accurate way to measure angles, and more importantly, Javascript trigonometry does not work in degrees, but radians.

Because of this we will need the following conversion functions:

`const degreesToRadians = degrees => degrees * (Math.PI / 180)`

const radiansToDegrees = radians => radians * (180 / Math.PI)

Then all we need to do is put the rest of the equation into a function making use of Javascript’s Math trigonometry utilities.

The function will look something like this:

const centralSubtendedAngle = (locationX, locationY) => {

const locationXLatRadians = degreesToRadians(locationX.latitude)

const locationYLatRadians = degreesToRadians(locationY.latitude)return radiansToDegrees(

Math.acos(

Math.sin(locationXLatRadians) * Math.sin(locationYLatRadians) +

Math.cos(locationXLatRadians) *

Math.cos(locationYLatRadians) *

Math.cos(

degreesToRadians(

Math.abs(locationX.longitude - locationY.longitude)

)

)

)

)

}

The next thing we need to calculate is the **distance **of the arc the **centralSubtendedAngle —** this is known as the “Great circle distance”

This can be described as followed:

`2 * PI * {radius of the earth} * ({central subtended angle} / 360)`

The earth is roughly considered to have a radius of 6371kms

We can then turn this into a piece of Javascript:

const earthRadius = 6371const greatCircleDistance = angle => 2 * Math.PI * earthRadius * (angle / 360)const distanceBetweenLocations = (locationX, locationY) =>

greatCircleDistance(centralSubtendedAngle(locationX, locationY))

Calling **distanceBetweenLocations **is as follows:

const NewYork = {latitude: 40.7128, longitude: 74.0060}

const Sydney = {latitude: -33.8688, longitude: -151.2093}console.log(distanceBetweenLocations(NewYork, Sydney))

A final note here — because of the distortion of the earth this method is not 100% accurate, however it is usually only off by less than 0.05%