@erexstudio/geo-span-measure

1.0.1 • Public • Published

Logo

@erexstudio/geo-span-measure

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

NPM Version NPM Downloads npm bundle size NPM License

Installation

This is a Node.js module available through the npm registry.

Before installing, download and install Node.js. 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 npm init command.

Installation is done using the npm install command:

  $ npm install @erexstudio/geo-span-measure

Features

  • 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.

Usage

  • Back-End ( Node JS )
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`);
  • Front-End ( React JS, Next JS, etc )
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");

Function Explanation

The @erexstudio/geo-span-measure 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

const distanceInKilometers = haversineDistance(
  [37.7749, -122.4194],
  [34.0522, -118.2437]
);
console.log(`Distance in kilometers: ${distanceInKilometers} km`);

Earth Radius

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.

License

This project is licensed under the ISC License - 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.

Package Sidebar

Install

npm i @erexstudio/geo-span-measure

Weekly Downloads

0

Version

1.0.1

License

ISC

Unpacked Size

6.92 kB

Total Files

3

Last publish

Collaborators

  • erexstudio