Skip to content

Instantly share code, notes, and snippets.

@rintoandrews90
Created August 11, 2019 06:30
Show Gist options
  • Save rintoandrews90/9a5015147e5fffaceda1ac7084b22267 to your computer and use it in GitHub Desktop.
Save rintoandrews90/9a5015147e5fffaceda1ac7084b22267 to your computer and use it in GitHub Desktop.
Node Callback with API
const geoCode = (address,callback) => {
const gioCodeURL = 'https://api.mapbox.com/geocoding/v5/mapbox.places/'+ encodeURIComponent(address) + '.json?access_token=pk.eyJ1IjoicmludG8iLCJhIjoiY2p6NmllczBiMG1nbjNucG5qZjVqOXZwZSJ9.qu2LrgatHMB-so2E4oHaFA&limit=1'
request({url:gioCodeURL,json:true}, (error,response) => {
if (error) {
callback('Unable to connect to wheather API',undefined)
} else if (response.body.features.length == 0){
callback('Unable to find location, try another location',undefined)
} else {
const latitude = response.body.features[0].center[0]
const longitude = response.body.features[0].center[1]
callback(undefined,{latitude:latitude,longitude:longitude})
}
})
}
geoCode("Kerala", (error,data) => {
console.log('Err',error)
console.log('Data',data)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment