Geocoding with Node.js

Geocoding with Node.js Banner

When working with geographic positions in your program, it is nice to have a good geocoder to convert positions to addresses and addresses to positions. There are several services you can use to do this including Google Maps, Mapzen, OpenStreetMap, and many more. Luckily there is an easy to use library which allows you to not be tied to a single service while still being able to geocode effectively. Enter the node-geocoder library from Nicolas Chaulet. By using a single interface to build your application around, a quick configuration change can allow you to change the geocoding service being used.

Installation

You can easily install the node-geocoder library in your node project using npm:

npm install node-geocoder

Setup

To begin using node-geocoder we need to require it in our project, set our configuration, and use the constructor to instantiate a node-geocoder instance.

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

const geocoderOptions = {
  provider: 'google', // Which geocoding service to user
  httpAdapter: 'https', // Default
  apiKey: 'YOUR_API_KEY' // Most of the providers require an API key
};

const geocoder = NodeGeocoder(geocoderOptions);

Geocoding

Now that we have our geocoder set up we can take our address and get geographic points related to it using the geocode() method. (NOTE: node-geocoder provides both callback and promise style usage. I prefer using promises and particularly async/await)


const address = '2600 Clifton Ave, Cincinnati, Ohio 45220'; // Go Bearcats!

const results = await geocoder.geocode(address);

// Results:
[
  {
    formattedAddress: '2600 Clifton Ave, Cincinnati, OH 45220, USA',
    latitude: 39.13108,
    longitude: -84.5177841,
    extra: {
      googlePlaceId: 'ChIJ9UVJaIuzQYgRk19WdBEEhzI',
      confidence: 1,
      premise: null,
      subpremise: null,
      neighborhood: 'The Heights',
      establishment: null
    },
    administrativeLevels: {
      level2long: 'Hamilton County',
      level2short: 'Hamilton County',
      level1long: 'Ohio',
      level1short: 'OH'
    },
    streetNumber: '2600',
    streetName: 'Clifton Avenue',
    city: 'Cincinnati',
    country: 'United States',
    countryCode: 'US',
    zipcode: '45220',
    provider: 'google'
  }
]

Reverse Geocoding

If we want to take this the other way, we can provide latitude and longitude to get the related geographic positions. Using the same setup, we will instead call the reverse() method to get our results.


const position = { lat: 39.13108, lon: -84.5177841 };

const results = await geocoder.reverse(position);

// Results:
[
  { 
    formattedAddress: 'Tangeman University Center, 2766 UC MainStreet, Cincinnati, OH 45221, USA',
    latitude: 39.13177170000001,
    longitude: -84.5174673,
    extra: {
      googlePlaceId: 'ChIJt97tZIuzQYgRoFDFVTS4U8k',
      confidence: 1,
      premise: 'Tangeman University Center',
      subpremise: null,
      neighborhood: 'The Heights',
      establishment: null
    },
    administrativeLevels: {
      level2long: 'Hamilton County',
      level2short: 'Hamilton County',
      level1long: 'Ohio',
      level1short: 'OH'
    },
    streetNumber: '2766',
    streetName: 'UC MainStreet',
    city: 'Cincinnati',
    country: 'United States',
    countryCode: 'US',
    zipcode: '45221',
    provider: 'google'
  }
]