Geolocation Utilities

1.2.6 · active · verified Sun Apr 19

geolocation-utils is a JavaScript/TypeScript library designed for performing various calculations and conversions related to geographical locations. As of its latest stable release, version 1.2.6, published approximately a year ago, the library focuses on providing utility functions rather than direct device geolocation access. It enables conversion between common location formats like `[lon, lat]`, `{lat, lon}`, `{lat, lng}`, and `{latitude, longitude}`. Key functionalities include calculating distances and headings between points, moving a specified distance to a new location, determining bounding boxes around a list of locations, and checking if a location falls within a given polygon, circle, or bounding box. The library differentiates itself by using plain JavaScript objects for locations, which simplifies serialization and interoperability with other libraries and REST APIs. While there isn't a strict major release cadence, development appears active on GitHub with periodic updates.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic usage including format conversion, distance calculation, moving to a new point, and checking if a point is within a polygon.

import { 
  toLatLon, headingDistanceTo, moveTo, insidePolygon 
} from 'geolocation-utils';

// Convert various location formats
console.log(toLatLon([4, 51]));                        
// { lat: 51, lon: 4 }

// Calculate distance and heading, then move to a new location
const location1 = {lat: 51, lon: 4};
const location2 = {lat: 51.001, lon: 4.001 };
const { heading, distance } = headingDistanceTo(location1, location2);
console.log({ heading, distance }); 
// { heading: 32.18..., distance: 131.52... }

const newLocation = moveTo(location1, {heading, distance});
console.log(newLocation);
// { lat: 51.001000..., lon: 4.000997... }

// Check if a location is inside a polygon
const polygon = [
  [4.03146, 51.9644],
  [4.03151, 51.9643],
  [4.03048, 51.9627],
  [4.04550, 51.9600],
  [4.05279, 51.9605],
  [4.05215, 51.9619],
  [4.04528, 51.9614],
  [4.03146, 51.9644]
];
console.log(insidePolygon([4.03324, 51.9632], polygon)); // true

view raw JSON →