geocoder
This project is derived from node-geocoder with focus on modern code with esm modules.
Features:
- multiple providers
- similar results for all providers
- modern code based on esm modules and native Promises with async/ await
- Typescript types
- http(s) agent by default for reusing tcp connections
- fetch based http adapter with timeout
- cascade providers and stop on first successful result
- combine search results from multiple providers
- configurable circuit breaker which stops calling geocoder e.g. if request limit is exhausted.
- extensive test-suite with examples for getting started
- GeoJSON, GPX formatters
supported providers
usage
forward geocoding
import { fetchAdapter, OsmGeocoder } from '@spurreiter/geocoder'
const adapter = fetchAdapter()
const geocoder = new OsmGeocoder(adapter, { language: 'en', limit: 5, referer: 'https://mysite' })
const results = await geocoder.forward('135 pilkington avenue, birmingham')
reverse geocoding
const results = await geocoder.reverse({ lat: 40.714232, lng: -73.9612889 })
cascade
Allows to sequentially ask various geocoders for results. Successful results from the first geocoder are returned.
Works with forward and reverse geocoding.
import { Cascade, fetchAdapter, HereGeocoder, OsmGeocoder } from '@spurreiter/geocoder'
const language = "es"
const geocoders = [
new HereGeocoder(adapter, { apiKey: 'your-api-key', language }),
new OsmGeocoder(adapter, { language, referer: 'https://mysite' })
]
const cascade = new Cascade(geocoders)
const results = await cascade.forward('135 pilkington avenue, birmingham')
combine
Combine results from various geocoders into one result set.
Works with forward and reverse geocoding.
import { Combine, fetchAdapter, HereGeocoder, OsmGeocoder } from '@spurreiter/geocoder'
const geocoders = [
new HereGeocoder(adapter, { apiKey: 'your-api-key' }),
new OsmGeocoder(adapter, { referer: 'https://mysite' })
]
const combine = new Combine(geocoders)
const results = await combine.forward({ address: '135 pilkington avenue, birmingham', language: 'es' })
formatters
Formatters allow to format the geocoder result object to various formats like geoJson or gpx.
geoJsonFormatter
The output of the GeoJSON formatter is according to RFC-7946 and geocodejson-spec.
import { geoJsonFormatter } from '@spurreiter/geocoder'
const query = '135 pilkington avenue, birmingham'
const results = await geocoder.forward(query)
const geoJson = geoJsonFormatter(results, {query})
gpxFormatter
import { gpxFormatter } from '@spurreiter/geocoder'
const query = '135 pilkington avenue, birmingham'
const results = await geocoder.forward(query)
const gpx = gpxFormatter(results)
contributing
If you are missing a provider, which should be part of this project, please consider forking this project and filing a pull-request.
license
MIT licensed