Created
August 11, 2019 06:30
-
-
Save rintoandrews90/9a5015147e5fffaceda1ac7084b22267 to your computer and use it in GitHub Desktop.
Node Callback with 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 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