Last active
November 9, 2019 21:19
-
-
Save usmansbk/32cef941e0d7620152d34f323c5393df to your computer and use it in GitHub Desktop.
Geocoder port using GeoDB cities API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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