node-geocoder
Node library for geocoding and reverse geocoding. Can be used as a nodejs library
Installation (nodejs library)
npm install node-geocoder
Usage example
var NodeGeocoder = require('node-geocoder');
var options = {
provider: 'google',
httpAdapter: 'https',
apiKey: 'YOUR_API_KEY',
formatter: null
};
var geocoder = NodeGeocoder(options);
geocoder.geocode('29 champs elysée paris', function(err, res) {
console.log(res);
});
geocoder.geocode('29 champs elysée paris')
.then(function(res) {
console.log(res);
})
.catch(function(err) {
console.log(err);
});
[{
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'
}]
Advanced usage (only google, here, mapquest, and opencage providers)
geocoder.geocode({address: '29 champs elysée', country: 'France', zipcode: '75008'}, function(err, res) {
console.log(res);
});
geocoder.geocode({address: '29 champs elysée', countryCode: 'fr', minConfidence: 0.5, limit: 5}, function(err, res) {
console.log(res);
});
geocoder.reverse({lat:45.767, lon:4.833}, function(err, res) {
console.log(res);
});
geocoder.reverse({lat:45.767, lon:4.833})
.then(function(res) {
console.log(res);
})
.catch(function(err) {
console.log(err);
});
geocoder.batchGeocode(['13 rue sainte catherine', 'another adress'], function (results) {
console.log(results) ;
});
var HttpsAdapter = require('node-geocoder/lib/httpadapter/httpsadapter.js')
var httpAdapter = new HttpsAdapter(null, {
headers: {
'user-agent': 'My application <email@domain.com>',
'X-Specific-Header': 'Specific value'
}
});
var geocoder = NodeGeocoder({
provider: 'google',
httpAdapter: httpAdapter
});
Geocoder Provider
google
: GoogleGeocoder. Supports address geocoding and reverse geocoding. Use options.clientId
and options.apiKey
(privateKey) for business licence. You can also use options.language
and options.region
to specify language and region, respectively. Note that 'https' is required when using an apiKeyhere
: HereGeocoder. Supports address geocoding and reverse geocoding. You must specify options.appId
and options.appCode
with your license keys. You can also use options.language
, options.politicalView
(read about political views here), options.country
, and options.state
.freegeoip
: FreegeoipGeocoder. Supports IP geocodingdatasciencetoolkit
: DataScienceToolkitGeocoder. Supports IPv4 geocoding and address geocoding. Use options.host
to specify a local instanceopenstreetmap
: OpenStreetMapGeocoder. Supports address geocoding and reverse geocoding. You can use options.language
and options.email
to specify a language and a contact email address.
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 specify options.apiKey
nominatimmapquest
: Same geocoder as openstreetmap
, but queries the MapQuest servers. You need to specify options.apiKey
opencage
: OpenCage Geocoder. Uses multiple open sources. Supports address and reverse geocoding. You need to specify options.apiKey
smartyStreet
: Smarty street geocoder (US only), you need to specify options.auth_id
and options.auth_token
geocodio
: Geocodio, Supports address geocoding and reverse geocoding (US only)yandex
: Yandex support address geocoding, you can use options.language
to specify languageteleport
: Teleport supports city and urban area forward and reverse geocoding; for more information, see Teleport API documentationopendata france
: OpendataFranceGeocoder supports forward and reverse geocoding in France; for more information, see OpendataFrance API documentation
Http adapter
https
: This adapter uses the Https nodejs library (default)http
: This adapter uses the Http nodejs library
You can specify request timeout using paramater options.timeout
Formatter
gpx
: format result using GPX formatstring
: format result to an String array (you need to specify options.formatterPattern
key)
%P
country%p
country code%n
street number%S
street name%z
zip code%T
State%t
state code
More
options
node-geocoder-cli
You can use node-geocoder-cli to geocode in shell
Extending node geocoder
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; },
}
Contributing
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
.