npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@erexstudio/geo-span-measure

v1.0.1

Published

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

Downloads

18

Readme

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.