-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgeocode.js
executable file
·45 lines (35 loc) · 1.11 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
41
42
43
44
45
#!/Users/`whoami`/local/bin/node
var http = require('http'),
loc = process.argv.slice(2).join(' '),
formatLoc = function (place) {
if ( ! place ) {
console.log('Where?');
process.exit();
}
return place.replace( /\s/g,'+');
},
options = {
host: 'maps.googleapis.com',
port: 80,
path: '/maps/api/geocode/json?address='+ formatLoc(loc) +'&sensor=false',
headers: {'Content-type': 'aplication/json'}
};
var req = http.get(options, function(res) {
var geocodeData = '';
//console.log('STATUS: ' + res.statusCode);
//console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk, foo) {
geocodeData += chunk ;
});
res.on('end', function () {
var data = JSON.parse( geocodeData );
if (! data.results.length ) {
console.log("No results for " + loc + ".")
return;
}
console.log (data.results[0].formatted_address );
console.log( data.results[0].geometry.location );
});
});
req.end();