udemy-node-weather-app/geocode/geocode.js
2017-11-21 00:34:29 -05:00

22 lines
744 B
JavaScript

const request = require('request');
var geocodeAddress = (address, callback) => {
var encodedAddress = encodeURIComponent(address);
request({
url: `https://maps.googleapis.com/maps/api/geocode/json?address=${encodedAddress}`,
json: true
}, (error, response, body) => {
if (error) {
callback("Unable to connect to Google Servers.");
} else if (body.status === "ZERO_RESULTS"){
callback("Unable to find that Address.");
} else if (body.status === "OK"){
callback(undefined, {
address: body.results[0].formatted_address,
latitude: body.results[0].geometry.location.lat,
longitude: body.results[0].geometry.location.lng
});
}
});
}
module.exports = { geocodeAddress }