Urgent.News

the world's headlines, one feed

Editions

Tech

How to Calculate Distance Between Two GPS Coordinates in JavaScript

When working with maps, GPS data, or location-based applications, one of the most common questions is: How far apart are these two coordinates? Suppose we have two locations: Point A Latitude: 40.7128 Longitude: -74.0060 Point B Latitude: 34.0522 Longitude: -118.2437 These are approximately New York City and Los Angeles. We can't simply subtract the latitude and longitude values and treat them…

Calculating the distance between two GPS coordinates in JavaScript can be achieved using the Haversine formula. This formula helps determine the great-circle distance between two points on a sphere, such as the Earth. To implement this, we first need to convert the latitude and longitude values from degrees to radians. This is because JavaScript's trigonometric functions, such as sin, cos, and atan2, require input in radians.

Here's a step-by-step guide to creating a JavaScript function that calculates the distance between two GPS coordinates using the Haversine formula:

1. **Degrees to Radians Conversion**: JavaScript's trigonometric functions expect input in radians. Therefore, we need a helper function to convert degrees to radians. This can be done using the formula: radians = degrees * π / 180. Implement this as follows:

```javascript

function toRadians(degrees) {

return degrees * Math.PI / 180;

}

```

2. **Haversine Formula Implementation**: Once we have our conversion function, we can implement the Haversine formula. This formula requires the latitude and longitude of two points, as well as the Earth's radius. The radius of the Earth is approximately 6,371.0088 kilometers. The formula itself involves several trigonometric operations and can be broken down into several steps:

- Calculate the differences in latitude and longitude, converting them to radians.

- Use these differences along with the original latitude values to calculate an intermediate value (a) using the Haversine formula.

- Finally, calculate the distance by multiplying the Earth's radius by the square root of 'a'.

Here's the complete function:

```javascript

function distanceBetweenCoordinates(lat1, lon1, lat2, lon2) {

const earthRadiusKm = 6371.0088; // Earth's radius in kilometers

const dLat = toRadians(lat2 - lat1);

const dLon = toRadians(lon2 - lon1);

const lat1Rad = toRadians(lat1);

const lat2Rad = toRadians(lat2);

const a = Math.sin(dLat / 2) ** 2 +

Math.cos(lat1Rad) * Math.cos(lat2Rad) *

Math.sin(dLon / 2) ** 2;

const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

return earthRadiusKm * c; // Distance in kilometers

}

```

3. **Using the Function**: To use this function, simply call it with the latitude and longitude of two points. For example, to find the distance between New York City and Los Angeles:

```javascript

const distance = distanceBetweenCoordinates(40.7128, -74.0060, 34.0522, -118.2437);

console.log(distance);

```

The result is approximately 3,935 kilometers, depending on the Earth's radius value and the level of rounding applied.

It's important to note that while this method works well for most applications, it assumes the Earth is a perfect sphere. In reality, the Earth's shape is more complex, and for highly precise calculations, more sophisticated models may be necessary.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — it may contain errors, so check the original before relying on it.

Read the original at dev.to →

More in Tech

Semiconductor Exports Drive Record August Trade

Exports in early August topped $21.3 billion, setting a new all-time high for the August 1 to 10 period. Backed by a strong semiconductor market, overall exports surged more than 45% from a year…

  • Semiconductor exports surged 155.4% to $10.0 billion, accounting for 46.8% of total exports
  • Passenger car exports dropped 80.9% to $182 million due to production line shutdowns

Why We Moved Away From WordPress So Early

When Andrés and I started building websites, WordPress was one of the first things we tried. It made sense. There were themes, plugins for almost everything, and you could get a website running pretty…

  • Moved away from WordPress due to complexity and compatibility issues
  • Transitioned to building websites from scratch using React
  • Preferred custom solutions for businesses with unique needs