@shgysk8zer0/geoutils
v1.0.6
Published
Geo utils library for JavaScript
Maintainers
Readme
geoutils
A utility library for geolocation and geohashing in JavaScript.
Installation
npm install @shgysk8zer0/geoutilsUsage
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.
