-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapviz (5).html
267 lines (247 loc) · 9.91 KB
/
mapviz (5).html
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
265
266
267
<!DOCTYPE html>
<head lang="en">
<meta charset="utf-8">
<title>M A P</title>
<script src="jquery.js"></script>
<link rel="stylesheet" type="text/css" href="mapviz.css">
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.3/leaflet.css">
<link rel="stylesheet" href="http://leaflet.github.io/Leaflet.label/leaflet.label.css">
<script type="text/javascript" src="nicescroll.min.js"></script>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=false"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.3/leaflet.js"></script>
<script type="text/javascript" src="http://leaflet.github.io/Leaflet.label/leaflet.label.js"></script>
</head>
<body>
<div id="map"></div>
<div id="locations"></div>
<script type="text/javascript">
L.Map = L.Map.extend({
openPopup: function(popup) {
// this.closePopup();
this._popup = popup;
return this.addLayer(popup).fire('popupopen', {
popup: this._popup
});
}
});
var map = L.map('map').setView([15, -5], 2);
map.attributionControl.setPrefix('');
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png?{foo}', {foo: 'bar'}).addTo(map);
var geocoder;
function initialize() {
geocoder = new google.maps.Geocoder();
console.log("geocoder initialized");
}
function makeReadable(str) {
if ( (str == "U.S.") || (str == "America") ) {
return "United States";
}
return str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
}
var masterList = [];
var countries = [];
var citiesInUS = [];
var citiesNotInUS = [];
var NonUSorganizations = ["European Union", "Islamic State", "E.U.", "The British Foreign Office", "The Times of London", "Muslim Brotherhood", "Hamas", "Hezbollah", "European Council", "European Commission", "European Central Bank", "Syrian Observatory for Human Rights", "Atlantic"];
function getCountriesCities() {
$.ajax({
method: 'GET',
url: 'countries2cities.json',
dataType: 'json',
success: function(data) {
for (var key in data) {
if (data.hasOwnProperty(key)) {
masterList.push(key);
countries.push(key);
for (k = 0; k < data[key].length; k++) {
masterList.push(data[key][k]);
}
if (key == "United States") {
for (m = 0; m < data[key].length; m++) {
citiesInUS.push(data[key][m]);
}
}
if (key != "United States") {
for (n = 0; n < data[key].length; n++) {
citiesNotInUS.push(data[key][n]);
}
}
}
}
InfoFromFeed.getData();
}
});
}
var InfoFromFeed = (function() {
function getInfoFromFeed() {
$.ajax({
method: 'GET',
url: 'feed.txt',
dataType: 'json',
success: function(data) {
function hidePopups() {
for (var r = 0; r < document.getElementsByClassName('points').length; r++) {
if (document.getElementsByClassName('points')[r].dataset.leafletmarker) {
allMarkers[parseInt(document.getElementsByClassName('points')[r].dataset.leafletmarker)].closePopup();
}
}
}
$("#locations").append("<p id='locationsp' style='font-weight: bold; color: #126180; font-size: 20px; margin-bottom: 10px'>LOCATIONS</p>");
$("#locationsp").click(function() {
hidePopups();
map.setView(new L.LatLng(15, -5), 2, {
animation: true
});
});
var allLocations = [];
var organizedLocations = [];
var organizedLatLngs = [];
var allMarkers = [];
var articleCount = 0;
var pointCount = 0;
//make side navigation bar
for (var a = 0; a < data.posts.length; a++) { //for each article in feed.txt
var locationsInUl = [];
if (data.posts[a].locations.length != 0) { //if each article contains locations
$("#locations").append("<ul class='group' id=article"+articleCount+"></ul>");
$("#locations #article"+articleCount).append("<li class='clickable article' style='font-weight: bold; margin-bottom: 5px'>"+data.posts[a].title+"</li>");
for (var l = 0; l < data.posts[a].locations.length; l++) { //for each location in article
if ( (masterList.indexOf(makeReadable(data.posts[a].locations[l])) > -1) && (locationsInUl.indexOf(makeReadable(data.posts[a].locations[l])) == -1) && (NonUSorganizations.indexOf(makeReadable(data.posts[a].locations[l])) == -1) ) { //if location is valid
locationsInUl.push(makeReadable(data.posts[a].locations[l]));
allLocations.push(makeReadable(data.posts[a].locations[l]));
$("#locations #article"+articleCount).append("<li class='clickablee point' id="+pointCount+">"+makeReadable(data.posts[a].locations[l])+"</li>");
pointCount++;
}
if (l == (data.posts[a].locations.length - 1) ) { //if location is last in article
$("#locations #article"+articleCount).append("<br>");
}
}
organizedLocations.push(locationsInUl);
articleCount++;
}
}
//geocode and create markers for every point
//group markers into layers by article
var allArticles = document.getElementsByClassName('group');
var article = 0;
while (article < allArticles.length) { //for each article in feed.txt
var thisArticle = allArticles[article];
locationsInArticle = thisArticle.querySelectorAll('.point');
for (var loc = 0; loc < locationsInArticle.length; loc++) { //for each location in article
var thisLoc = locationsInArticle[loc];
var articleOrganizations = data.posts[parseInt(thisLoc.parentNode.id.replace( /^\D+/g, ''))].organizations;
var locationToUse;
for (var location = 0; location < thisLoc.parentNode.getElementsByClassName('point').length; location++) { //for each location in article
var theLocation = thisLoc.parentNode.getElementsByClassName('point')[location].innerHTML;
if (countries.indexOf(theLocation) > -1) {
locationToUse = thisLoc.innerHTML;
}
else if (countries.indexOf(theLocation) == -1) {
if ( (citiesNotInUS.indexOf(theLocation) > -1) && (citiesInUS.indexOf(theLocation) == -1) ) {
locationToUse = thisLoc.innerHTML;
}
else if ( (citiesNotInUS.indexOf(theLocation) == -1) && (citiesInUS.indexOf(theLocation) > -1) ) {
locationToUse = thisLoc.innerHTML + ', United States';
}
else if ( (citiesNotInUS.indexOf(theLocation) > -1) && (citiesInUS.indexOf(theLocation) > -1) ) {
NonUSOrgcount = 0;
if (articleOrganizations.length != 0) {
for (var org = 0; org < articleOrganizations.length; org++) {
if (NonUSorganizations.indexOf(articleOrganizations[org]) > -1) {
NonUSOrgcount += 1;
}
}
if (NonUSOrgcount == 0) {
locationToUse = thisLoc.innerHTML + ', United States';
}
else if (NonUSOrgcount > 0) {
locationToUse = thisLoc.innerHTML;
}
}
else {
locationToUse = thisLoc.innerHTML;
}
}
}
}
if (locationToUse == "United States, United States") {
locationToUse = "United States";
}
thisLoc.dataset.loc = locationToUse;
}
function chunk(array) {
var chunks = [];
var littleChunk = [];
var i = 0;
while (i < array.length) {
if ( (i == array.length-1) && (i % 8 != 7) ) {
chunks.push(littleChunk);
}
if (i % 8 != 7) {
littleChunk.push(array[i]);
}
if (i % 8 == 7) {
littleChunk.push(array[i]);
chunks.push(littleChunk);
littleChunk = [];
}
i++;
}
return chunks
}
var latlngsInArticle = [];
var chunkedLocationsInArticle = chunk(locationsInArticle);
for (var set = 0; set < chunkedLocationsInArticle.length; set++) { //for each set of locations in chunkedLocationsInArticle
var locationSet = chunkedLocationsInArticle[set];
for (var location = 0; location < locationSet.length; location++) { //for each location in locationSet
var thisPoint = locationSet[location];
var thePoint = thisPoint.innerHTML;
var latlng = [];
var thisPointLocation = thisPoint.dataset.loc;
function getAddress(thisPointLocation, loc) {setTimeout(function() {
geocoder.geocode( {'address': thisPointLocation}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
latlng = [];
lat = results[0].geometry.location.lat().toFixed(6);
lng = results[0].geometry.location.lng().toFixed(6);
latlng.push(lat);
latlng.push(lng);
console.log(latlng);
latlngsInArticle.push(latlng);
if (loc == locationsInArticle.length-1) {
console.log(latlngsInArticle);
organizedLatLngs.push(latlngsInArticle);
latlngsInArticle = [];
}
document.getElementById(thisPoint.id).dataset.position = latlng[0]+", "+latlng[1];
}
else if (status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT) {
console.log(status);
}
});
}, loc*6000);}
getAddress(thisPointLocation, loc);
}
}
article++;
if (article == allArticles.length) {
console.log(organizedLatLngs);
}
}
}
});
}
return {
getData: function() {
getInfoFromFeed(function(data){});
}
}
})();
$(document).ready(function() {
initialize();
getCountriesCities();
$("#locations").niceScroll({mousescrollstep:40, hidecursordelay: 200, scrollspeed: 40, cursorwidth: 7, cursorcolor: "#157195", cursoropacitymax: .7, cursoropacitymin: .3});
});
</script>
</body>
</html>