https://github.com/shgysk8zer0/geoutils
Geo utils library for JavaScript
https://github.com/shgysk8zer0/geoutils
geo geohash geoutils js
Last synced: 2 months ago
JSON representation
Geo utils library for JavaScript
- Host: GitHub
- URL: https://github.com/shgysk8zer0/geoutils
- Owner: shgysk8zer0
- License: mit
- Created: 2024-12-19T22:40:38.000Z (11 months ago)
- Default Branch: master
- Last Pushed: 2024-12-27T05:33:55.000Z (11 months ago)
- Last Synced: 2024-12-27T05:35:38.164Z (11 months ago)
- Topics: geo, geohash, geoutils, js
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/@shgysk8zer0/geoutils
- Size: 127 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: .github/CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# geoutils
A utility library for geolocation and geohashing in JavaScript.
[](https://github.com/shgysk8zer0/geoutils/actions/workflows/codeql-analysis.yml)


[](https://github.com/shgysk8zer0/geoutils/blob/master/LICENSE)
[](https://github.com/shgysk8zer0/geoutils/commits/master)
[](https://github.com/shgysk8zer0/geoutils/releases)
[](https://github.com/sponsors/shgysk8zer0)
[](https://www.npmjs.com/package/@shgysk8zer0/geoutils)


[](https://www.npmjs.com/package/@shgysk8zer0/geoutils)
[](https://github.com/shgysk8zer0)


[](https://twitter.com/shgysk8zer0)
[](https://liberapay.com/shgysk8zer0/donate "Donate using Liberapay")
- - -
- [Code of Conduct](./.github/CODE_OF_CONDUCT.md)
- [Contributing](./.github/CONTRIBUTING.md)
## Installation
```sh
npm install @shgysk8zer0/geoutils
```
## Usage
```javascript
import { getDistance, encodeGeohash, decodeGeohash, getGeohashDistance, checkLocation, checkGeohash, getGeohashBounds, estimateGeohashAccuracy } from '@shgysk8zer0/geoutils';
// Example usage
const distance = getDistance(
{ latitude: 40.7128, longitude: -74.0060 },
{ latitude: 34.0522, longitude: -118.2437 }
);
const geohash = encodeGeohash({ latitude: 40.7128, longitude: -74.0060 });
const coordinates = decodeGeohash(geohash);
const geohashDistance = getGeohashDistance('dr5ru7', '9q5ctr');
const isWithinRadius = checkLocation(
{ latitude: 40.7128, longitude: -74.0060 },
{ latitude: 34.0522, longitude: -118.2437 },
{ radius: 5000000 }
);
const geohashBounds = getGeohashBounds('dr5ru7');
const geohashAccuracy = estimateGeohashAccuracy('dr5ru7');
```
## API
### `getDistance(coords1, coords2, options)`
| Parameter | Type | Description |
| --- | --- | --- |
| `coords1` | `object` | Object containing `latitude` and `longitude` of the first location. |
| `coords1.latitude` | `number` | Latitude of the first location. |
| `coords1.longitude` | `number` | Longitude of the first location. |
| `coords2` | `object` | Object containing `latitude` and `longitude` of the second location. |
| `coords2.latitude` | `number` | Latitude of the second location. |
| `coords2.longitude` | `number` | Longitude of the second location. |
| `options` | `object` | Optional parameters. |
| `options.highAccuracy` | `boolean` | Whether to use high accuracy (Haversine formula) or lower accuracy approximation. |
Returns the distance in meters.
### `encodeGeohash(location, precision)`
| Parameter | Type | Description |
| --- | --- | --- |
| `location` | `object` | Object containing `latitude` and `longitude`. |
| `location.latitude` | `number` | Latitude of the location. |
| `location.longitude` | `number` | Longitude of the location. |
| `precision` | `number` | Desired geohash precision (length). |
Returns the geohash string.
### `decodeGeohash(geohash)`
| Parameter | Type | Description |
| --- | --- | --- |
| `geohash` | `string` | The geohash to parse. |
Returns an object with `latitude` and `longitude`.
### `getGeohashDistance(geohash1, geohash2, options)`
| Parameter | Type | Description |
| --- | --- | --- |
| `geohash1` | `string` | The first geohash. |
| `geohash2` | `string` | The second geohash. |
| `options` | `object` | Optional parameters. |
| `options.highAccuracy` | `boolean` | Whether to use high accuracy (Haversine formula) or lower accuracy approximation. |
Returns the distance in meters.
### `checkLocation(location, location2, options)`
| Parameter | Type | Description |
| --- | --- | --- |
| `location` | `object` | Object containing `latitude` and `longitude` of the target location. |
| `location.latitude` | `number` | Latitude of the target location. |
| `location.longitude` | `number` | Longitude of the target location. |
| `location2` | `object` | Object containing `latitude` and `longitude` of the reference location. |
| `location2.latitude` | `number` | Latitude of the reference location. |
| `location2.longitude` | `number` | Longitude of the reference location. |
| `options` | `object` | Optional parameters. |
| `options.radius` | `number` | Radius in meters to check within. |
| `options.highAccuracy` | `boolean` | Whether to use high accuracy (Haversine formula) or lower accuracy approximation. |
Returns `true` if the location is within the specified radius, `false` otherwise.
### `checkGeohash(geohash, coords, options)`
| Parameter | Type | Description |
| --- | --- | --- |
| `geohash` | `string` | The geohash to check against. |
| `coords` | `object` | Object containing `latitude` and `longitude` of the coordinates. |
| `coords.latitude` | `number` | Latitude of the coordinates. |
| `coords.longitude` | `number` | Longitude of the coordinates. |
| `options` | `object` | Optional parameters. |
| `options.highAccuracy` | `boolean` | Whether to use high accuracy (Haversine formula) or lower accuracy approximation. |
| `options.radius` | `number` | Radius in kilometers to check within. |
Returns `true` if the geohash is within the specified radius, `false` otherwise.
### `getGeohashBounds(geohash)`
| Parameter | Type | Description |
| --- | --- | --- |
| `geohash` | `string` | The geohash to get bounds for. |
Returns an object with `latitude` and `longitude` bounds.
### `estimateGeohashAccuracy(geohash)`
| Parameter | Type | Description |
| --- | --- | --- |
| `geohash` | `string` | The geohash to estimate accuracy for. |
Returns the estimated accuracy in meters.