-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgeocode.js
40 lines (33 loc) · 1.16 KB
/
geocode.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const request = require('request');
var latitude = body.results[0].geometry.location.lat;
var longitude = body.results[0].geometry.location.lng;
var geocodeAddress = function(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)
{
console.log('Unable to connect to the servers');
}
else if(body.status === "ZERO_RESULTS")
{
console.log('Bad request..');
}
else if(body.error_message === "Invalid request. Missing the 'address', 'components', 'latlng' or 'place_id' parameter."){
console.log('Google maps are unable to provide services...');
}
else if(body.status === "OK")
{
callback(undefined,
{
latitude: body.results[0].geometry.location.lat,
longitude: body.results[0].geometry.location.lng
});
}
});
};
module.exports.geocodeAddress = geocodeAddress;