RintoAndrews
8/11/2019 - 6:30 AM

Node Callback with API

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)
})