Skip to content

Instantly share code, notes, and snippets.

@usmansbk
Last active November 9, 2019 21:19
Show Gist options
  • Save usmansbk/32cef941e0d7620152d34f323c5393df to your computer and use it in GitHub Desktop.
Save usmansbk/32cef941e0d7620152d34f323c5393df to your computer and use it in GitHub Desktop.
Geocoder port using GeoDB cities API
const HOST = process.env.GEODB_HOST;
const KEY = process.env.GEODB_API_KEY;
const printableNumber = num => (num < 0 ? "" : "+") + num;
const parseLocation = location => {
const { lat, lng } = location;
return `${printableNumber(lat)}${printableNumber(lng)}`
};
export default function(location) {
const locationId = parseLocation(location);
const url = `https://${HOST}/v1/geo/locations/${locationId}/nearbyCities?limit=1&radius=100`;
const headers = {
"x-rapidapi-host": HOST,
"x-rapidapi-key": KEY
};
return fetch(url, {
method: 'GET',
headers
}).then(response => response.json())
.then(json => {
const { data, errors } = json;
if (errors) {
throw new Error(JSON.stringify(errors));
}
return data;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment