@erexstudio/geo-span-measure
Version:
A simple npm package for calculating distance between two coordinates using the Haversine formula.
117 lines (77 loc) • 4.73 kB
Markdown

A simple npm package for calculating distance between two coordinates using the Haversine formula.




This is a **<span style="color: #CB3837;">Node.js</span>** module available through the **<span style="color: #CB3837;">npm registry</span>**.
Before installing, **<span style="color: #CB3837;">download and install Node.js</span>**. Node.js 0.10 or higher is required.
If this is a brand new project, make sure to create a `package.json` first with the **<span style="color: #CB3837;">npm init</span>** command.
Installation is done using the **<span style="color: #CB3837;">npm install</span>** command:
```bash
$ npm install @erexstudio/geo-span-measure
```
- Simple npm installation for quick integration.
- Accepts latitude and longitude coordinates as arrays.
- Calculates distances in kilometers, miles, or meters.
- Robust error handling for smooth user experience.
- Returns distance with two decimal places.
- Uses the Haversine formula for accurate results.
- Suitable for **Node.js**, **React.js**, **Next.js**, and various **JavaScript** applications.
- **<span style="color: #CB3837;">Back-End</span>** ( Node JS )
```javascript
const haversineDistance = require("@erexstudio/geo-span-measure");
const coord1 = [latitude1, longitude1]; // Replace with actual coordinates
const coord2 = [latitude2, longitude2]; // Replace with actual coordinates
// Calculate distance in kilometers (default)
const distanceInKilometers = haversineDistance(coord1, coord2);
console.log(`Distance in kilometers: ${distanceInKilometers} km`);
// Calculate distance in miles
const distanceInMiles = haversineDistance(coord1, coord2, "miles");
console.log(`Distance in miles: ${distanceInMiles} miles`);
// Calculate distance in meters
const distanceInMeters = haversineDistance(coord1, coord2, "meters");
console.log(`Distance in meters: ${distanceInMeters} meters`);
```
- **<span style="color: #CB3837;">Front-End</span>** ( React JS, Next JS, etc )
```javascript
import haversineDistance from "@erexstudio/geo-span-measure";
// Example coordinates
const coord1 = [latitude1, longitude1]; // Replace with actual coordinates
const coord2 = [latitude2, longitude2]; // Replace with actual coordinates
// Calculate distance in kilometers (default)
const distanceInKilometers = haversineDistance(coord1, coord2);
// Calculate distance in miles
const distanceInMiles = haversineDistance(coord1, coord2, "miles");
// Calculate distance in meters
const distanceInMeters = haversineDistance(coord1, coord2, "meters");
```
The **<span style="color: #CB3837;">@erexstudio/geo-span-measure</span>** package takes two sets of coordinates and an optional unit parameter (defaulting to **kilometers** if not specified). It uses the Haversine formula to calculate the distance between the two points on the Earth's surface.
## Parameters
- `coord1` : Array containing the **latitude** and **longitude** of the first coordinate.
- `coord2` : Array containing the **latitude** and **longitude** of the second coordinate.
- `unit` : Optional string parameter specifying the desired **unit** for the output distance (**'kilometers'**, **'miles'**, or **'meters'**).
## Return Value
The function returns the calculated distance between the **two coordinates** in the specified **unit**.
## Example
```js
const distanceInKilometers = haversineDistance(
[],
[]
);
console.log(`Distance in kilometers: ${distanceInKilometers} km`);
```
The function uses the following **Earth radius** for different **units**:
- `Kilometers` : 6357 km
- `Miles` : 3950 miles
- `Meters` : 6371000 meters
Users can specify the **unit** according to their preference.
This project is licensed under the **<span style="color: #CB3837;">ISC License</span>** - see the LICENSE file for details.
This README includes explanations for the parameters, return value, example usage, a note about distance formatting, and details about Earth radius for different units. Adjustments can be made based on your specific requirements and preferences.