udemy-node-weather-app/geocode/geocode.js

22 lines
744 B
JavaScript
Raw Normal View History

2017-11-21 05:34:29 +00:00
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 }