Skip to content

Instantly share code, notes, and snippets.

@juanpujol
Last active May 16, 2016 16:01
Show Gist options
  • Save juanpujol/3704ca2566dce6587debe002236cf0b5 to your computer and use it in GitHub Desktop.
Save juanpujol/3704ca2566dce6587debe002236cf0b5 to your computer and use it in GitHub Desktop.
Node.js OpenStreetMap geocode and reverse geocode API wrapper
'use strict'
const request = require('request-promise');
module.exports = Geocoder;
function Geocoder() {
if (!(this instanceof Geocoder)) {
return new Geocoder();
}
this.url = 'https://nominatim.openstreetmap.org';
}
Geocoder.prototype.geocode = function(options) {
if (!options.q) { throw new Error('q parameter is required') }
let rOptions = {
uri: `${this.url}/search`,
qs: {
q: options.q,
addressdetails: options.addressdetails || 1,
polygon_geojson: options.polygon_geojson || 1,
format: 'json'
},
json: true
}
return request(rOptions);
}
Geocoder.prototype.reverse = function(options) {
if (!options.lat) { throw new Error('lat parameter is required') }
if (!options.lon) { throw new Error('lon parameter is required') }
let rOptions = {
uri: `${this.url}/reverse`,
qs: {
lat: options.lat,
lon: options.lon,
addressdetails: options.addressdetails || 1,
polygon_geojson: options.polygon_geojson || 1,
format: 'json'
},
json: true
}
return request(rOptions);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment