haversine-geolocation
Get distances between two points or get closest position to current point. Based on the Haversine Formula
Last updated 2 years ago by daniilsydorenko .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install haversine-geolocation 
SYNC missed versions from official npm registry.

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

npm install haversine-geolocation --save

Basic usage

Importing:

import HaversineGeolocation from 'haversine-geolocation';

API:

Is geolocation available:

HaversineGeolocation.isGeolocationAvailable()
    .then(data => {
        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) 2018 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.

Current Tags

  • 1.3.0                                ...           latest (2 years ago)

14 Versions

  • 1.3.0                                ...           2 years ago
  • 1.2.2                                ...           2 years ago
  • 1.2.1                                ...           2 years ago
  • 1.1.2                                ...           2 years ago
  • 1.1.1                                ...           2 years ago
  • 1.1.0                                ...           2 years ago
  • 1.0.7                                ...           2 years ago
  • 1.0.6                                ...           2 years ago
  • 1.0.5                                ...           2 years ago
  • 1.0.4                                ...           2 years ago
  • 1.0.3                                ...           2 years ago
  • 1.0.2                                ...           2 years ago
  • 1.0.1                                ...           2 years ago
  • 1.0.0                                ...           2 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 8
Dependencies (10)
Dev Dependencies (4)
Dependents (0)
None

Copyright 2014 - 2017 © taobao.org |