Skip to content

Commit e5b1364

Browse files
committed
ch19: added Google geocoding utility.
1 parent 789837b commit e5b1364

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

ch19/lib/geocode.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
var http = require('http');
2+
3+
module.exports = function(query, cb){
4+
5+
var options = {
6+
hostname: 'maps.googleapis.com',
7+
path: '/maps/api/geocode/json?address=' +
8+
encodeURIComponent(query) + '&sensor=false',
9+
};
10+
11+
http.request(options, function(res){
12+
var data = '';
13+
res.on('data', function(chunk){
14+
data += chunk;
15+
});
16+
res.on('end', function(){
17+
data = JSON.parse(data);
18+
if(data.results.length){
19+
cb(null, data.results[0].geometry.location);
20+
} else {
21+
cb("No results found.", null);
22+
}
23+
});
24+
}).end();
25+
};

0 commit comments

Comments
 (0)