JSPM

offline-geocode-city

1.0.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 102
  • Score
    100M100P100Q79592F
  • License MIT

217 kB, tiny offline reverse geocoding library that works anywhere, browser, Node.js, web worker. High performance (S2 cell based). Looks up country and nearest city, given GPS coordinates

Package Exports

  • offline-geocode-city

Readme

offline-geocode-city

217 kB, tiny offline reverse geocoding library that works anywhere, browser, Node.js, web worker. High performance (S2 cell based). Locally looks up country and nearest city, given GPS coordinates.

Features

  • ✅ Reverse geocodes offline
  • ✅ Works in all JS environments (browser, Node.js, service worker etc.)
  • ✅ Not only Country and ISO2, but also nearest city geocoded
  • ✅ Only 217 kB nano sized (ESM, gizpped) (alternatives are ~`20 MiB`)
  • ✅ Smallest library size for city reverse geocoding on NPM
  • ✅ Best performance for reverse geocoding using S2 cell on NPM
  • ✅ Lookup time (on M1 Max): 0.035 ms (!) (direct cell hit), 0.11 ms (2nd order range hit), 4.87 ms (worst case)
  • ✅ Available as a simple API
  • ✅ Tree-shakable and side-effect free
  • ✅ First class TypeScript support

Example usage (API)

Setup

  • yarn: yarn add offline-geocode-city
  • npm: npm install offline-geocode-city

ESM

import { getNearestCity } from 'offline-geocode-city'

const nearestCity = getNearestCity(48.3243193, 11.658644)

deepEqual(nearestCity, {
    cityName: 'Ismaning',
    countryIso2: 'DE',
    countryName: 'Germany'
})

This library can be used to lookup the nearest city in-browser using the GeoLocation web api (offline):

const getLocation = (): Promise<GeolocationResult> =>
    new Promise((resolve, reject) => {
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(resolve, reject);
        } else {
            reject(new Error('Geolocation is not supported by this browser.'));
        }
    });

const position = await getLocation()

 if (position instanceof Error) {
    console.error(position.message);
} else {

    console.log(`Latitude: ${(position.coords.latitude)}`);
    console.log(`Longitude: ${(position.coords.longitude)}`);

    const nearestCity = getNearestCity(position.coords.latitude, position.coords.longitude)

    console.log('nearestCity', nearestCity)
}