23 lines
807 B
JavaScript
23 lines
807 B
JavaScript
const request = require('request')
|
|
|
|
const geocode = (address, callback) => {
|
|
const url = 'https://api.mapbox.com/geocoding/v5/mapbox.places/' + encodeURIComponent(address) + '.json?access_token=pk.eyJ1IjoiamFzb24zMmRldiIsImEiOiJjazFsNHd5djcwMXptM2htbW8zM3MyZGxuIn0.-TtNNGFysQPQRfGR1P8DUA&limit=1'
|
|
|
|
request({ url, json: true }, (error, {body}) => {
|
|
console.log(body)
|
|
if (error) {
|
|
callback('Unable to connect to location services!', undefined)
|
|
} else if (body.features.length === 0) {
|
|
callback('Unable to find location. Try another search.', undefined)
|
|
} else {
|
|
callback(undefined, {
|
|
latitude: body.features[0].center[1],
|
|
longitude: body.features[0].center[0],
|
|
location: body.features[0].place_name
|
|
})
|
|
}
|
|
})
|
|
}
|
|
|
|
module.exports = geocode
|