JSPM

offline-geocode-city

1.0.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 128
  • Score
    100M100P100Q79920F
  • 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). 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`)
  • ✅ World record smallest size for city reverse geocoding
  • ✅ World record performance for reverse geocoding using S2 cell
  • ✅ 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)
}