-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapviz (3).html
261 lines (248 loc) · 10.7 KB
/
mapviz (3).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
<!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 = [];
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++) {
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 allLatLngs = [];
var LocationsInUl = [];
var allMarkers = [];
var counterArticles = 0;
var counterPoints = 0;
for (var i = 0; i < data.posts.length; i++) { //for each article in feed.txt
LocationsInUl = [];
if (data.posts[i].locations.length != 0) { //if article contains locations
$("#locations").append("<ul class='group' id=article"+counterArticles+"></ul>");
$("#locations #article"+counterArticles).append("<li class='clickable articles' style='font-weight: bold; margin-bottom: 5px'>"+data.posts[i].title+"</li>");
for (var j = 0; j < data.posts[i].locations.length; j++) { //for each location in article
if ( (masterList.indexOf(makeReadable(data.posts[i].locations[j])) > -1) && (LocationsInUl.indexOf(makeReadable(data.posts[i].locations[j])) == -1) ) { //if location is valid
LocationsInUl.push(makeReadable(data.posts[i].locations[j]));
$("#locations #article"+counterArticles).append("<li class='clickable points' id="+counterPoints+">"+makeReadable(data.posts[i].locations[j])+"</li>");
counterPoints += 1;
}
if (j == (data.posts[i].locations.length - 1)) { //if location is last location in article
$("#locations #article"+counterArticles).append("<br>");
}
}
counterArticles += 1;
}
}
var allPoints = document.getElementsByClassName('points');
var counter = 0;
for (var i = 0; i < allPoints.length; i++) {
var latlng = [];
thispoint = allPoints[i];
function getAddress(thispoint) {setTimeout(function () {
var locationsInArticle = [];
var locationToUse;
for (var q = 0; q < thispoint.parentNode.getElementsByClassName('points').length; q++) {
var thelocation = thispoint.parentNode.getElementsByClassName('points')[q].innerHTML;
locationsInArticle.push(thelocation);
if ( ( ( (locationsInArticle.indexOf("United States") > -1) || (citiesInUS.indexOf(thelocation) > -1) ) || (citiesNotInUS.indexOf(thelocation) == -1) ) && (countries.indexOf(thelocation) == -1) ) {
locationToUse = thispoint.innerHTML + ', United States';
}
if ( (countries.indexOf(thelocation) > -1) || (citiesInUS.indexOf(thelocation) == -1) ) {
locationToUse = thispoint.innerHTML;
}
}
console.log(locationToUse);
geocoder.geocode( { 'address': locationToUse}, 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);
allLatLngs.push(latlng);
allLocations.push(thispoint.innerHTML);
var newMarker = new L.marker(latlng).addTo(map);
allMarkers.push(newMarker);
newMarker.bindPopup(thispoint.innerHTML);
newMarker.on('mouseover', function (e) {
this.openPopup();
});
newMarker.on('mouseout', function (e) {
this.closePopup();
});
document.getElementById(thispoint.id).dataset.leafletmarker = thispoint.id;
document.getElementById(thispoint.id).dataset.position = latlng[0]+", "+latlng[1];
thispoint.onclick = function(e) {
hidePopups();
allMarkers[parseInt(e.target.dataset.leafletmarker)].openPopup();
var Position = e.target.dataset.position;
if (Position) {
map.setView(new L.LatLng(parseFloat(Position.split(',')[0]), parseFloat(Position.split(',')[1])), 8, {
animation: true
});
}
};
if (thispoint.dataset.position) {
counter += 1;
}
if (counter == allPoints.length) { //when all markers have been placed
var allUls = document.getElementsByClassName('articles');
var LatLngsinLocation = [];
var LatLngsInUl = [];
var allUlpolylineArrays = []
for (var k = 0; k < allUls.length; k++) { //for each article
LatLngsInUl = [];
thisul = allUls[k];
thisulLocations = thisul.parentNode.getElementsByClassName('points');
for (var j = 0; j < thisulLocations.length; j++) { //for each location in article
if (thisulLocations[j].dataset.position) {
lat = parseFloat(thisulLocations[j].dataset.position.split(",")[0]);
lng = parseFloat(thisulLocations[j].dataset.position.split(",")[1]);
LatLngsInUl.push([lat, lng]);
}
}
var polylineArray = []
for (var m = 0; m < LatLngsInUl.length; m++) {
polylineArray.push(LatLngsInUl[m]);
}
allUlpolylineArrays.push(polylineArray);
var color = (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256));
var drawpolyline = (L.polyline(allUlpolylineArrays[k], {color: 'rgb(' + color + ')', weight: 3, opacity: 0.6})).bindLabel(thisul.innerHTML).addTo(map);
polylineArrayToUse = JSON.stringify(allUlpolylineArrays[k]);
document.getElementById(thisul.parentNode.id).dataset.polyline = polylineArrayToUse;
document.getElementById(thisul.parentNode.id).dataset.color = color;
thisul.onclick = function(e) {
map.setView(new L.LatLng(15, -5), 2, {
animation: true
});
hidePopups();
var Polyline = JSON.parse(e.target.parentNode.dataset.polyline);
var Color = e.target.parentNode.dataset.color;
setTimeout(function() {
if (Polyline) {
function drawMarkers() {
for (var s = 0; s < e.target.parentNode.getElementsByClassName('points').length; s++) {
allMarkers[parseInt(e.target.parentNode.getElementsByClassName('points')[s].dataset.leafletmarker)].openPopup();
}
}
newpolyline = (L.polyline(Polyline, {color: 'rgb(' + Color + ')', weight: 8, opacity: 1})).bindLabel(e.target.innerHTML).addTo(map);
map.fitBounds(newpolyline.getBounds());
setTimeout(function() {map.removeLayer(newpolyline);}, 1000);
drawMarkers();
setTimeout(function() {
newpolyline = (L.polyline(Polyline, {color: 'rgb(' + Color + ')', weight: 8, opacity: 1})).bindLabel(e.target.innerHTML).addTo(map);
map.fitBounds(newpolyline.getBounds());
setTimeout(function() {map.removeLayer(newpolyline);}, 1000);
}, 2000);
}
}, 1000);
};
}
}
}
else {
console.log(status);
console.log(thispoint.innerHTML);
}
});
}, i*1000);}
getAddress(thispoint);
}
}
});
}
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>