haversine-geolocation
Version:
Get distances between two points or get closest position to current point. Based on the Haversine Formula
171 lines (146 loc) • 5.25 kB
Markdown
Haversine-Geolocation
========
[](https://nodei.co/npm/haversine-geolocation/)
[](https://opensource.org/licenses/MIT) [](https://travis-ci.org/DaniilSydorenko/haversine-geolocation)
- [Introduction](
- [Haversine Formula](
- [Pseudocode](
- [Installation](
- [Basic usage](
- [Importing](
- [API](
- [Is geolocation available](
- [Calculate distance between two points](
- [Calculate the closest position to user](
- [License](
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:
<img width="439" alt="haversine" src="https://user-images.githubusercontent.com/2789198/27240436-e9a459da-52d4-11e7-8f84-f96d0b312859.png">

```code()
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
```
```bash
npm i haversine-geolocation -S
```
```javascript
import HaversineGeolocation from 'haversine-geolocation';
```
```javascript
HaversineGeolocation.isGeolocationAvailable()
.then(data => {
const currentPoint = {
latitude: data.coords.latitude,
longitude: data.coords.longitude,
accuracy: data.coords.accuracy
};
});
```
```javascript
(async () => {
const data = await HaversineGeolocation.isGeolocationAvailable();
const currentPoint = {
latitude: data.coords.latitude,
longitude: data.coords.longitude,
accuracy: data.coords.accuracy
};
})();
```
```javascript
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
```
Will return all existed properties with a small nested object haversine: { distance: val, measurement: 'val' }
```javascript
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'
);
});
```
```json
{
"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.