-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapviz.html
208 lines (197 loc) · 7.83 KB
/
mapviz.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
<!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" />
<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>
</head>
<body>
<div id="map"></div>
<div id="locations"></div>
<script type="text/javascript">
var map = L.map('map').setView([0, -20], 2);
//var map = L.map('map').setView([42.3598, -71.0921], 5);
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.") {
return "United States";
}
return str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
}
var masterList = [];
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);
for (k = 0; k < data[key].length; k++) {
masterList.push(data[key][k]);
}
}
}
InfoFromFeed.getData();
}
});
}
var InfoFromFeed = (function() {
function getInfoFromFeed() {
$.ajax({
method: 'GET',
url: 'feed.txt',
dataType: 'json',
success: function(data) {
$("#locations").append("<p id='locationsp' style='font-weight: bold; color: #126180; font-size: 20px; margin-bottom: 10px'>LOCATIONS</p>");
$("#locationsp").click(function() {
map.setView(new L.LatLng(0, -20), 2, {
animation: true
});
});
var allLocations = [];
var allLatLngs = [];
var LocationsInUl = [];
var counterArticles = 0;
var counterPoints = 0;
for (var i = 0; i < data.posts.length; i++) { //for each article in feed.txt
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) { //if location is valid
$("#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 () {
geocoder.geocode( { 'address': thispoint.innerHTML}, 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);
if (!(allLocations.indexOf(thispoint.innerHTML) > -1)) {
allLocations.push(thispoint.innerHTML);
var newMarker = new L.marker(latlng).addTo(map);
newMarker.bindPopup(thispoint.innerHTML);
newMarker.on('mouseover', function (e) {
this.openPopup();
});
newMarker.on('mouseout', function (e) {
this.closePopup();
});
}
document.getElementById(thispoint.id).dataset.position = latlng[0]+", "+latlng[1];
thispoint.onclick = function(e) {
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 = [];
LocationsInUl = [];
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) {
LocationsInUl.push(thisulLocations[j].innerHTML);
lat = parseFloat(thisulLocations[j].dataset.position.split(",")[0]);
lng = parseFloat(thisulLocations[j].dataset.position.split(",")[1]);
LatLngsInUl.push([lat, lng]);
//LatLngsInUl.push(new L.LatLng(lat, lng));
}
}
var polylineArray = []
for (var m = 0; m < LatLngsInUl.length; m++) {
polylineArray.push(LatLngsInUl[m]);
}
allUlpolylineArrays.push(polylineArray);
function getRandomColor() {
var letters = '0123456789ABCDEF'.split('');
var color = '#';
for (var i = 0; i < 6; i++ ) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
var color = getRandomColor();
var drawpolyline = (L.polyline(allUlpolylineArrays[k])).addTo(map);
drawpolyline.setStyle({
color: color,
weight: 3,
opacity: 1
});
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) {
var Polyline = JSON.parse(e.target.parentNode.dataset.polyline);
var Color = e.target.parentNode.dataset.color;
if (Polyline) {
map.fitBounds((L.polyline(Polyline)).addTo(map).getBounds());
}
};
}
}
}
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>