Node library for geocoding and reverse geocoding. Can be used as a nodejs library or on command line
npm install -g node-geocoder
geocoder --provider google 'Fornino, 187 Bedford Ave, Brooklyn, NY 11211'
geocoder-reverse 48.858887 2.294486npm install node-geocoder
var geocoderProvider = 'google';
var httpAdapter = 'http';
// optionnal
var extra = {
apiKey: 'YOUR_API_KEY', // for Mapquest, OpenCage, Google Premier
formatter: null // 'gpx', 'string', ...
};
var geocoder = require('node-geocoder')(geocoderProvider, httpAdapter, extra);
// Using callback
geocoder.geocode('29 champs elysée paris', function(err, res) {
console.log(res);
});
// Or using Promise
geocoder.geocode('29 champs elysée paris')
.then(function(res) {
console.log(res);
})
.catch(function(err) {
console.log(err);
});
// output :
[{
latitude: 48.8698679,
longitude: 2.3072976,
country: 'France',
countryCode: 'FR',
city: 'Paris',
zipcode: '75008',
streetName: 'Champs-Élysées',
streetNumber: '29',
state: 'Île de France',
stateCode: 'IDF'
}]
## Advanced usage (only google provider)
geocoder.geocode({address: '29 champs elysée', country: 'France', zipcode: '75008'}, function(err, res) {
console.log(res);
});
// Reverse example
// Using callback
geocoder.reverse({lat:45.767, lon:4.833}, function(err, res) {
console.log(res);
});
// Or using Promise
geocoder.reverse({lat:45.767, lon:4.833})
.then(function(res) {
console.log(res);
})
.catch(function(err) {
console.log(err);
});
// Batch geocode
geocoder.batchGeocode(['13 rue sainte catherine', 'another adress'], function (results) {
// Return an array of type {error: false, value: []}
console.log(results) ;
});google: GoogleGeocoder. Supports address geocoding and reverse geocoding. Useextra.clientIdandextra.apiKey(privateKey) for business licence. You can also useextra.languageandextra.regionto specify language and region, respectively. Note that 'https' is required when using an apiKeyfreegeoip: FreegeoipGeocoder. Supports IP geocodingdatasciencetoolkit: DataScienceToolkitGeocoder. Supports IPv4 geocoding and address geocoding. Useextra.hostto specify a local instanceopenstreetmap: OpenStreetMapGeocoder. Supports address geocoding and reverse geocoding. You can useextra.languageandextra.emailto specify a language and a contact email address.- For
geocode, you can use an object as value, specifying one or several parameters from https://wiki.openstreetmap.org/wiki/Nominatim#Parameters - For
reverse, you can use additional parameters from https://wiki.openstreetmap.org/wiki/Nominatim#Parameters_2
- For
mapquest: MapQuestGeocoder. Supports address geocoding and reverse geocoding. Needs an apiKeyopenmapquest: Open MapQuestGeocoder (based on OpenStreetMapGeocoder). Supports address geocoding and reverse geocoding. Needs an apiKeyagol: ArcGis Online Geocoding service. Supports geocoding and reverse. Requires a client_id & client_secret and 'https' http adaptertomtom: TomTomGeocoder. Supports address geocoding. You need to specifyextra.apiKeynominatimmapquest: Same geocoder asopenstreetmap, but queries the MapQuest servers.opencage: OpenCage Geocoder. Uses multiple open sources. Supports address and reverse geocoding. You need to specifyextra.apiKeysmartyStreet: Smarty street geocoder (US only), you need to specifyextra.auth_idandextra.auth_tokengeocodio: Geocodio, Supports address geocoding and reverse geocoding (US only)
http: This adapter uses the Http nodejs library (default)https: This adapter uses the Https nodejs library
gpx: format result using GPX formatstring: format result to an String array (you need to specifyextra.formatterPatternkey)%Pcountry%pcountry code%nstreet number%Sstreet name%zzip code%TState%tstate code
You can improve this project by adding new geocoders or http adapters.
To run tests just npm test.
To check code style install jshint and just run jshint lib test.
You can add new geocoders by implementing the two methods geocode and reverse:
var 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
var formatter = {
format: function(data) { return formattedData; },
}
