cityjs
Simply finds the nearest city to lat/long position from a tiny database of about 50,000 cities. It's super fast and requires no remote api's (can be used offline).
Why?
Querying remote api's to find information about cities is not always an option. Maybe you have an offline raspberry pi with gps? Or maybe you have a reactive web app that needs to quickly lookup nearest cities without constantly pinging a remote api.
Data
This package uses data found here: http://download.geonames.org/export/dump
By default the dataset of cities with populations > 5,000 is used. If you need to also lookup cities with smaller populations, please build cityjs from source.
This data set puts the cityjs package at around 1.8MB. This may not be ideal for some web apps, but it's small enough for most applications.
Usage
nearestCity({ latitude: number, longitude: number})
Returns a the nearest city to latitude, longitude. Provides city name, country code, lat, long, and distance from queried coordinates.
import { nearestCity } from 'cityjs'
const cityNearMe = nearestCity({ latitude: 44.0618643, longitude: -121.3188065 });
console.log(cityNearMe);
nearestCities({ latitude: number, longitude: number}, k: number)
Similar to nearestCity, but returns an array of cities of length k sorted from nearest to farthest.
import { nearestCities } from 'cityjs'
const citiesNearMe = nearestCities({ latitude: 44.0618643, longitude: -121.3188065 }, 3);
console.log(citiesNearMe);
Credit/Thanks
- This package was heavily influence by the python package citipy.
- Also credit to kd-tree-javascript module which is bundled with cityjs. This is where the magic happens.
TODO