nchaulet / node-geocoder

nodejs geocoding library
http://nchaulet.github.io/node-geocoder/
MIT License
926 stars 213 forks source link
address-geocoding geocoder javascript node-geocoder nodejs-library

node-geocoder

Test Status npm version

Node library for geocoding and reverse geocoding. Can be used as a nodejs library

Installation (nodejs library)

npm install node-geocoder

Usage example

const NodeGeocoder = require('node-geocoder');

const options = {
  provider: 'google',

  // Optional depending on the providers
  fetch: customFetchImplementation,
  apiKey: 'YOUR_API_KEY', // for Mapquest, OpenCage, APlace, Google Premier
  formatter: null // 'gpx', 'string', ...
};

const geocoder = NodeGeocoder(options);

// 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'
  }
];

Advanced usage (only google, here, mapquest, locationiq, and opencage providers)

const res = await geocoder.geocode({
  address: '29 champs elysée',
  country: 'France',
  zipcode: '75008'
});

// OpenCage advanced usage example
const res = await geocoder.geocode({
  address: '29 champs elysée',
  countryCode: 'fr',
  minConfidence: 0.5,
  limit: 5
});

// Reverse example

const res = await geocoder.reverse({ lat: 45.767, lon: 4.833 });

// Batch geocode

const results = await geocoder.batchGeocode([
  '13 rue sainte catherine',
  'another address'
]);

// Set specific http request headers:
const nodeFetch = require('node-fetch');

const geocoder = NodeGeocoder({
  provider: 'google',
  fetch: function fetch(url, options) {
    return nodeFetch(url, {
      ...options,
      headers: {
        'user-agent': 'My application <email@domain.com>',
        'X-Specific-Header': 'Specific value'
      }
    });
  }
});

Geocoder Providers (in alphabetical order)

Fetch option

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, ...

Formatter

More

Playground

You can try node-geocoder here http://node-geocoder.herokuapp.com/

Command line tools

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:

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;
  }
};

Contributing

You can improve this project by adding new geocoders.

To run tests just npm test.

To check code style just run npm run lint.