Node library for geocoding and reverse geocoding.
This is based on the original node-geocoder library and is fully rewritten in TypeScript. The API is not fully backwards compatible with the JS version.
There have been, and will be a few major changes.
- Fully re-written in TypeScipt
- Restructured project
- Callbacks have been removed in favor of promises
- Here provider has been updated with a
version
option (defaults to v7) - Sinon and Chai were removed as dependencies in favor of Just Jest™
- add
options.formatterOptions
to be passed to whateveroptions.formatter
is provided - Potentially create general option shape that can map to all provider options
- Update
reverse()
params to uselongitude
&latitude
instead oflon
/lat
- Remove instaces of
_reverse()
in favor ofreverse()
- Create provider-specific option types for each
geocode
andreverse
method - Create integration tests for every provider
- Update APIs (ie SmartyStreets is now Smarty and has a different endpoint)
- Add reverse geocoding to TomTom provider
- Add reverse geocoding to Yandex provider
- Add RapidAPI
- Add Maps.co
- Add Geonames
- Migrate NominatimMapQuestProvider to Nominatim (with
mapquest
andosm
server options)
npm install node-geocoder
const createGeocoder = require('node-geocoder-ts');
const geocoder = createGeocoder({
provider: 'google',
// Optional depending on the providers
fetch: customFetchImplementation,
apiKey: 'YOUR_API_KEY', // for Mapquest, OpenCage, Google Premier
formatter: null // 'gpx', 'string', ...
});
// Using callback
const res = await geocoder.geocode('29 champs elysée paris');
// output :
[
{
latitude: 48.8698679,
longitude: 2.3072976,
country: 'France',
countryCode: 'FR',
city: 'Paris',
zipcode: '75008',
streetName: 'Champs-Élysées',
streetNumber: '29',
administrativeLevels: {
level1long: 'Île-de-France',
level1short: 'IDF',
level2long: 'Paris',
level2short: '75'
},
provider: 'google'
}
];
const result = await geocoder.geocode({
address: '29 champs elysée',
country: 'France',
zipcode: '75008'
});
// OpenCage advanced usage example
const result = await geocoder.geocode({
address: '29 champs elysée',
countryCode: 'fr',
minConfidence: 0.5,
limit: 5
});
// Reverse example
const result = await geocoder.reverse({ lat: 45.767, lon: 4.833 });
// Batch geocode
const batchResult = await geocoder.batchGeocode([
'13 rue sainte catherine',
'another address'
]);
// Set specific http request headers:
const nodeFetch = require('node-fetch');
const geocoder = createGeocoder({
provider: 'google',
fetch: function fetch(url, options) {
return nodeFetch(url, {
...options,
headers: {
'user-agent': 'My application <email@domain.com>',
'X-Specific-Header': 'Specific value'
}
});
}
});
Service | Provider | Forward Geocoding | Reverse Geocoding | Authentication |
---|---|---|---|---|
ArcGis | agol |
✅ | ✅ | clientId , clientSecret |
Data Science Toolkit | datasciencetoolkit |
✅ | ✅ | |
FreeGeoIP.net | freegeoip |
✅ (IP only) | ||
Geocodio | geocodio |
✅ (US & CA) | ✅ (US & CA) | |
google |
✅ | ✅ | apiKey |
|
Here | here |
✅ | ✅ | apiKey |
LocationIQ | locationiq |
✅ | ✅ | apiKey |
Mapbox | mapbox |
✅ | ✅ | apiKey |
MapQuest | mapquest |
✅ | ✅ | apiKey |
OpenCage | opencage |
✅ | ✅ | apiKey |
OpenDataFrance | opendatafrance |
✅ | ✅ | apiKey |
[OpenStreetMap] | openstreetmap |
✅ | ✅ | |
PickPoint | pickpoint |
✅ | ✅ | apiKey |
Smarty | smartystreets |
✅ | authId , authToken |
|
Teleport | teleport |
✅ | ✅ | |
TomTom | tomtom |
✅ | apiKey |
|
VirtualEarth | virtualearth |
✅ | ✅ | apiKey |
Yandex | yandex |
✅ | apiKey |
With the options.fetch
you can provide your own method to fetch data. This method should be compatible with the Fetch API.
This allow you to specify a proxy to use, a custom timeout, specific headers, ...
gpx
: format result using GPX formatstring
: format result to an String array (you need to specifyoptions.formatterPattern
key)%P
country%p
country code%n
street number%S
street name%z
zip code%T
State%t
state code%c
City
You can add new geocoders by implementing the two methods geocode
and reverse
:
const geocoder = {
geocode: function(value, callback) { ... },
reverse: function(query, callback) { var lat = query.lat; var lon = query.lon; ... }
}
You can also add formatter implementing the following interface
const formatter = {
format: function(data) {
return formattedData;
}
};
You can improve this project by adding new geocoders.
To run tests just npm test
.
To check code style just run npm run lint
.