1
0
This commit is contained in:
Jason Williams
2019-08-25 14:57:02 -06:00
parent 051270a264
commit bc68915cbc
2 changed files with 40 additions and 14 deletions

View File

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