Giter VIP home page Giter VIP logo

haversine-geolocation's Introduction

Haversine-Geolocation

NPM

License: MIT Travis CI

Introduction

If you need to calculate the distance between two points or you want to find closest from position to your current location let me introduce haversine-geolocation module. It based on the Haversine Formula:

haversine

Formula

haversine_2

Pseudocode

dlon = lon2 - lon1 
dlat = lat2 - lat1 
a = (sin(dlat/2))^2 + cos(lat1) * cos(lat2) * (sin(dlon/2))^2 
c = 2 * atan2( sqrt(a), sqrt(1-a) ) 
d = R * c (where R is the radius of the Earth)

R = 6367 km OR 3956 mi

Installation

npm i haversine-geolocation -S

Basic usage

Importing:

import HaversineGeolocation from 'haversine-geolocation';

API:

Is geolocation available:

Promise

HaversineGeolocation.isGeolocationAvailable()
    .then(data => {
        const currentPoint = {
            latitude: data.coords.latitude,
            longitude: data.coords.longitude,
            accuracy: data.coords.accuracy
        };
    });

Async await

	(async () => {
        const data = await HaversineGeolocation.isGeolocationAvailable();
        const currentPoint = {
            latitude: data.coords.latitude,
            longitude: data.coords.longitude,
            accuracy: data.coords.accuracy
        };
	})();

Calculate distance between two points:

const points = [
    {
        latitude: 61.5322204,
        longitude: 28.7515963
    },
    {
        latitude: 51.9971208,
        longitude: 22.1455439
    }
];
 
// Distance in miles
HaversineGeolocation.getDistanceBetween(points[0], points[1], 'mi'); // 704.1 mi
 
// Distance in meters
HaversineGeolocation.getDistanceBetween(points[0], points[1], 'm'); // 1133062.7 m
 
// Distance in kilometers(default value)
HaversineGeolocation.getDistanceBetween(points[0], points[1]); // 1133.1 km

Calculate the closest position to user:

Will return all existed properties with a small nested object haversine: { distance: val, measurement: 'val' }

const locationPoints = [
    {
        id: 1,
        title: 'Point 1',
        latitude: 61.5322204,
        longitude: 28.7515963
    },
    {
        id: 2,
        title: 'Point 2',
        latitude: 51.9971208,
        longitude: 22.1455439
    },
    {
        id: 3,
        title: 'Point 3',
        latitude: 45.3571207,
        longitude: 30.3435456
    }
];
 
HaversineGeolocation.isGeolocationAvailable()
    .then(data => {
        const currentPoint = {
            latitude: data.coords.latitude,
            longitude: data.coords.longitude,
            accuracy: data.coords.accuracy
        };
        
        HaversineGeolocation.getClosestPosition(
            currentPoint, 
            locationPoints,
            'mi'
        );
    });

Expected response:

    {
        "id": 3,
        "title": "Point 3",
        "latitude": 45.3571207,
        "longitude": 30.3435456,
        "haversine": {
            "distance": 49,
            "measurement": "mi"
        }
    }

License

The MIT License (MIT)

Copyright (c) 2020 Daniil Sydorenko

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

haversine-geolocation's People

Contributors

daniilsydorenko avatar dansydorenko avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

haversine-geolocation's Issues

Getting error on import to Node.JS

Hi,
Using haversine-geolocation as following:
const { HaversineGeolocation} = require('haversine-geolocation');

Getting the following error on server start:
ReferenceError: window is not defined
at Object. (XXXXXXX\node_modules\haversine-geolocation\dist\build.js:1:224)

Update dependencies

This package depends on a lot of outdated packages that are either in maintenance mode, or not being maintained. Installation yields this log:


warning haversine-geolocation > sinon > @sinonjs/formatio > [email protected]: This package has been deprecated in favour of @sinonjs/samsam
warning haversine-geolocation > karma > log4js > [email protected]: CircularJSON is in maintenance only, flatted is its successor.
warning haversine-geolocation > karma > log4js > [email protected]: All versions below 4.0.1 of Nodemailer are deprecated. See https://nodemailer.com/status/
warning haversine-geolocation > karma > socket.io > engine.io > [email protected]: New code is available at github.com/uNetworking/uWebSockets.js
warning haversine-geolocation > karma > log4js > nodemailer > [email protected]: This project is unmaintained
warning haversine-geolocation > karma > log4js > nodemailer > [email protected]: If using 2.x branch, please upgrade to at least 2.1.6 to avoid a serious bug with socket data flow and an import issue introduced in 2.1.0
warning haversine-geolocation > karma > log4js > loggly > request > [email protected]: Use uuid module instead
warning haversine-geolocation > karma > log4js > nodemailer > mailcomposer > [email protected]: This project is unmaintained
warning haversine-geolocation > karma > log4js > loggly > request > hawk > [email protected]: This version is no longer maintained. Please upgrade to the latest version.
warning haversine-geolocation > karma > log4js > loggly > request > hawk > [email protected]: This version is no longer maintained. Please upgrade to the latest version.
warning haversine-geolocation > karma > log4js > loggly > request > hawk > cryptiles > [email protected]: This version is no longer maintained. Please upgrade to the latest version.
warning haversine-geolocation > karma > log4js > loggly > request > hawk > [email protected]: This version is no longer maintained. Please upgrade to the latest version.
warning haversine-geolocation > karma > log4js > loggly > request > hawk > sntp > [email protected]: This version is no longer maintained. Please upgrade to the latest version.
warning haversine-geolocation > karma > log4js > loggly > request > hawk > boom > [email protected]: This version is no longer maintained. Please upgrade to the latest version.
[2/4] ๐Ÿšš  Fetching packages...
[3/4] ๐Ÿ”—  Linking dependencies...
warning "haversine-geolocation > [email protected]" has unmet peer dependency "webpack@^1 || ^2 || ^3".
warning "haversine-geolocation > [email protected]" has unmet peer dependency "webpack@^1.0.0 || ^2.0.0 || ^3.0.0 || ^4.0.0".
warning "haversine-geolocation > karma-webpack > [email protected]" has unmet peer dependency "webpack@^1.0.0 || ^2.0.0 || ^3.0.0".

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.