-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
70 lines (55 loc) · 1.99 KB
/
index.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
var fs = require('fs');
var secret = JSON.parse(fs.readFileSync('data/secret.json', encoding='utf8'));
var foursquare = (require('foursquarevenues'))(secret.CLIENT_ID, secret.CLIENT_SECRET);
var params = [{
"intent": "browse",
"limit" : 50,
"sw":"52.34389,4.89724",
"ne": "52.37355,4.92476"
}];
var finalParams = []
function squaring(params, callback){
var paramsTotal = params.length,
paramsParsed = 0,
toSquaring = [];
params.forEach(function(d){
foursquare.getVenues(d, function(error, data) {
if (!error) {
if(data.response.venues.length > 45){
var sw = d.sw.split(","),
ne = d.ne.split(",");
var diffLat = (parseFloat(ne[0]) - parseFloat(sw[0]))/2,
diffLng = (parseFloat(ne[1]) - parseFloat(sw[1]))/2;
// calculate the new bounding boxes
var newLat = parseFloat(sw[0]) + diffLat,
newLng = parseFloat(sw[1]) + diffLng;
toSquaring.push({"sw": newLat + "," + sw[1], "ne": ne[0] + "," + newLng,"intent": "browse","limit" : 50})
toSquaring.push({"sw":newLat + "," + newLng,"ne": ne[0] + "," + ne[1],"intent": "browse","limit" : 50})
toSquaring.push({"sw":sw[0] + "," + sw[1], "ne": newLat + "," + newLng,"intent": "browse","limit" : 50})
toSquaring.push({"sw":sw[0] + "," + newLng, "ne": newLat + "," + ne[1],"intent": "browse","limit" : 50})
}else{
finalParams.push(d)
}
paramsParsed++
if(paramsParsed == paramsTotal){
if(toSquaring.length){
squaring(toSquaring)
}else{
callback(finalParams)
}
}
}else{
callback(error)
}
});
})
}
squaring(params, function(data){
fs.writeFile("data/grid.json", JSON.stringify(data, null, 2), function(err) {
if(err) {
console.log(err);
} else {
console.log("The file was saved!");
}
});
})