-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
264 lines (232 loc) · 8.34 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
var map;
var obsMarker;
var popups = [];
var popup;
var current_location;
var majorCities = {"New York": [42.3482, -75.1890],
"Chicago": [41.790636, -87.60104],
"Los Angeles": [34.0500, -118.2500],
"San Francisco": [37.7833, -122.4167],
"Philadelphia": [39.9500, -75.1667],
"Houston": [29.7628, -95.3831]
};
$("#speed").change(function (e) {
updatePopups();
});
$("#cities").change(function (e) {
$("#coords").val(majorCities[$("#cities").val()]);
});
$("#help").click(function(e) {
$("#help").hide();
});
$("#help-btn").click(function(e) {
console.log('hi');
$("#help").toggle();
});
$("#moveUser").click(function(e) {
current_location = LatLngFromInput();
obsMarker.setLatLng(current_location);
});
$("#moveNews").click(function(e) {
getNewsFrom4D(LatLngFromInput());
});
$("#swap").click(function(e) {
var newsLoc = popup.getLatLng();
var markLoc = current_location;
current_location = newsLoc;
obsMarker.setLatLng(newsLoc);
getNewsFrom4D(markLoc);
});
function LatLngFromInput() {
var coords = $("#coords").val().split(",");
console.log(coords.map(Number));
return new L.LatLng(coords[0], coords[1]);
}
$(document).ready(function(){
current_location = new L.LatLng(41.790636, -87.60104);
map = L.map('map').setView(current_location, 5);
L.tileLayer('http://{s}.tiles.mapbox.com/v3/jdangerx.k7m7mjbo/{z}/{x}/{y}.png', {
attribution: 'Map data © <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="http://mapbox.com">Mapbox</a>',
maxZoom: 18
}).addTo(map);
obsMarker = L.marker(current_location, {draggable: true, title: "You are here."});
obsMarker.addTo(map);
obsMarker.on('dragend', function(e) {
current_location = e.target._latlng;
var lng = current_location.lng;
var lat = current_location.lat;
updatePopups();
});
map.on('click', function(e) {
var targetPos = e.latlng;
getNewsFrom4D(targetPos);
});
// setup with new york, chicago, LA, philadelphia, san francisco, houston
// majorCities.forEach(function(city) {
// var targetPos = new L.LatLng(city[0], city[1]);
// var dist = haversine(current_location, targetPos);
// var travelTime = dist / $("#speed").val() | 0; // days
// getNewsFrom4D(targetPos);
// });
});
function getGLocFromPos(targetPos, callback) {
var lat = targetPos.lat;
var lng = targetPos.lng;
$.get("http://api.tiles.mapbox.com/v4/geocode/mapbox.places-v1/"+lng+","+lat+".json", {
access_token: mapboxToken
}, function(res) {
var features = res.features; // todo: parse features
var city = features.filter(function(feature){
return feature.id.slice(0, 4) == "city";
});
if (city.length > 0) {
city = city[0].text.toLowerCase();
} else {
city = void 0;
}
var province = features.filter(function(feature){
return feature.id.slice(0, 8) == "province";
});
if (province.length > 0) {
province = province[0].text.toLowerCase();
} else {
province = void 0;
}
// if (res.features.length >= 4) {
// var province = res.features[res.features.length-2].text.toLowerCase();
// var city = res.features[0].text.toLowerCase();
// }
city = newYorkSpecialCase(city);
callback(city, province);
}, "JSON");
return '("ILLINOIS")';
}
function newYorkSpecialCase(city) {
var boroughsEtc = ["manhattan", "brooklyn", "queens", "the bronx",
"staten island", "new york", "elmont", "old wbury",
"ridgewood", "bronx", "far rockaway", "atlantic beach"];
if (boroughsEtc.indexOf(city) == -1) {
return city;
} else {
return "new york city";
}
}
function getDateFromTravelTime(travelTime) {
var date = new Date();
var curday = date.getDate();
date.setDate(curday-travelTime);
var yearStr = ""+date.getFullYear();
var month = date.getMonth()+1;
var monthStr = month < 10? "0"+month : ""+month;
var day = date.getDate();
var dayStr = day < 10? "0"+day : ""+day;
var datestr = yearStr+monthStr+dayStr;
return datestr;
}
function getNewsFrom4D(targetPos){
var dist = haversine(current_location, targetPos);
var travelTime = dist / $("#speed").val() | 0; // in days
var articles;
getGLocFromPos(targetPos, function(city, province) {
var glocations = "glocations:"+'("'+city+'", "'+province+'")';
console.log(glocations);
$.get("http://api.nytimes.com/svc/search/v2/articlesearch.json", {
"api-key":API,
sort: "newest",
fq: glocations,
fl: "headline,web_url,keywords,pub_date",
end_date: getDateFromTravelTime(travelTime),
}, function(res) {
articles = res.response.docs;
// var popup = L.popup().setLatLng(targetPos, {closeOnClick: false});
$("#info").html("Distance: "+(dist|0)+" mi | Travel time: "+travelTime+" days");
popup = L.popup().setLatLng(targetPos, {closeOnClick: false});
popup.setContent(processArticles(articles, city, province));
popup.openOn(map);
// popups.push(popup);
// popup.on('popupclose', function(e) {
// var p = e.popup;
// popups.splice(popups.indexOf(p), 1);
// });
return articles;
}, "JSON");
});
}
function processArticles(articles, city, province) {
articles = articles.map(cleanKeywords);
articles = articles.map(function (article) {
article.locations = article.keywords.map(function(keyword) {
return keyword.value.toLowerCase();
});
article.states = [];
article.locations.forEach(function(location) {
if (STATES.indexOf(location) != -1) {
article.states.push(location);
}
});
return article;
});
articles.filter(function (article) {
if (article.states.length === 0) {
return true;
}
else {
return (article.states.indexOf(province) != -1);
}
});
var location = {city: city, province: province};
return prettify(articles, location);
}
function prettify(articles, location) {
locStr = location.city? location.city + ", " + location.province : location.province;
var artStrs = articles.map(function(article) {
var pub_date = article.pub_date.substring(0, 10);
return '<a href="' + article.web_url + '">' + article.headline.main + "</a><br>" + pub_date;
});
artStrs.unshift("<strong>"+locStr.toUpperCase()+"</strong>");
return artStrs.join("<br>");
}
function cleanKeywords(article) {
article.keywords = article.keywords.filter(function(val) { return val.name == "glocations"; });
return article;
}
function toRadians(deg) {
return deg * Math.PI / 180;
}
function haversine(latlng1, latlng2) {
// Takes two L.LatLngs are returns a distance
// var R = 6371; // km
var R = 3959; // miles
var phi1 = toRadians(latlng1.lat);
var phi2 = toRadians(latlng2.lat);
var dphi = toRadians(latlng2.lat - latlng1.lat);
var dlambda = toRadians(latlng2.lng - latlng1.lng);
var a = Math.sin(dphi/2) * Math.sin(dphi/2) +
Math.cos(phi1) * Math.cos(phi2) *
Math.sin(dlambda/2) * Math.sin(dlambda/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;
return d;
}
function updatePopups(){
// popups.forEach(function(popup) {
// var targetPos = popup.getLatLng();
// getNewsFrom4D(targetPos);
// });
var targetPos = popup.getLatLng();
getNewsFrom4D(targetPos);
}
var API = "240e8ee06f21f43d31b770c214bbf000:17:54902379";
var mapboxToken = "pk.eyJ1IjoiamRhbmdlcngiLCJhIjoic2t0aGl6SSJ9.e3gf0i6O2ecn2gwiii7yWw";
var STATES = ["Alabama", "Alaska", "Arizona", "Arkansas", "California",
"Colorado", "Connecticut", "Delaware", "Florida", "Georgia", "Hawaii",
"Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky",
"Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan",
"Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska",
"Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York",
"North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon",
"Pennsylvania", "Rhode Island", "South Carolina", "South Dakota",
"Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington",
"West Virginia", "Wisconsin", "Wyoming", "District of Columbia",
"Puerto Rico", "Guam", "American Samoa", "U.S. Virgin Islands", "Northern Mariana Islands "].map(function(val) {
return val.toLowerCase();});