-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·59 lines (51 loc) · 1.87 KB
/
Copy pathindex.js
File metadata and controls
executable file
·59 lines (51 loc) · 1.87 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env node
var fs = require('fs');
var https = require('https');
var csv =require('fast-csv');
var program = require('commander');
var querystring = require("querystring");
var sububrs = [];
program.arguments('<file>').action(function(file) {
readFile(file)
}).parse(process.argv);
function readFile(file) {
var stream = fs.createReadStream(file);
var csvStream = csv()
.on("data", function(data){
sububrs.push({id:data[0], address: data[2]});
})
.on("end", function(){
getGeo();
});
console.log("Start reading file...");
stream.pipe(csvStream);
}
function getGeo() {
for (var i = 0; i < sububrs.length; i++) {
(
function () {
var _i = i;
setTimeout(function () {
var url = 'https://maps.google.com/maps/api/geocode/json?'+querystring.stringify({address: sububrs[_i].address, key: 'AIzaSyD3jh46tgveH5e4D7hepU8Uice5wJk_bnQ'});
https.get(url, (response) => {
var body = '';
response.on('data', function(d) {
body += d;
});
response.on('end', function() {
var response = JSON.parse(body);
var result = response.results[0];
if (result) {
var lat = result.geometry.location.lat;
var long = result.geometry.location.lng;
console.log(sububrs[_i].id+","+sububrs[_i].address + "," + lat + "," + long);
}
});
}).on('error', (e) => {
console.error(e);
});
}, i*200);
}
)()
}
}