forked from cit-upenn/hw5-project-territorial-twitter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Leaflet-embed.html
208 lines (167 loc) · 5.02 KB
/
Leaflet-embed.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>
<html>
<head>
<title>Twitter Mood Map</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet/v0.7.7/leaflet.css" />
<script src="http://cdn.leafletjs.com/leaflet/v0.7.7/leaflet.js"></script>
<style>
#map {
width: 900px;
height: 600px;
}
.info {
padding: 6px 8px;
font: 14px/16px Arial, Helvetica, sans-serif;
background: white;
background: rgba(255,255,255,0.8);
box-shadow: 0 0 15px rgba(0,0,0,0.2);
border-radius: 5px;
}
.info h4 {
margin: 0 0 5px;
color: #777;
}
.legend {
text-align: left;
line-height: 18px;
color: #555;
}
.legend i {
width: 18px;
height: 18px;
float: left;
margin-right: 8px;
opacity: 0.7;
}
</style>
</head>
<body>
<div id="map"></div>
<script type="text/javascript" src="us-states-tweets-done.js"></script>
<script type="text/javascript">
// This is the coordinate for the US!
var map = L.map('map').setView([37.8, -96], 4);
L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token=pk.eyJ1Ijoid2VpeWlua28iLCJhIjoiY2locGFjZGpnMDN0enQ1bTJreXp3M21zNiJ9.929C4Qi49ear5wqJryXRsA', {
maxZoom: 18,
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>',
id: 'mapbox.light'
}).addTo(map);
L.geoJson(statesData).addTo(map);
// control that shows state info on hover, we can use these controls to see trends or w/e
var info = L.control();
info.onAdd = function (map) {
this._div = L.DomUtil.create('div', 'info');
this.update();
return this._div;
};
info.update = function (props) {
this._div.innerHTML = '<h4>US Search Term Comparison</h4>' + (props ?
'<b>' + props.name + '</b><br />' + props.Query1 + ' vs ' + props.Query2 + '</b><br />' + props.Query1 + ': ' +props.Q1Count + ' tweets' + '</b><br />' + props.Query2 + ': ' + props.Q2Count + ' tweets' + '</b><br />' + ' Query Ratio: </sup>' + props.tweets
: 'Hover mouse over a state to see details');
};
info.addTo(map);
// This function gets the colors for the different states
// Use colorbrewer for more interesting colors: http://colorbrewer2.org/
var tweets = 777;
function getColor(tweets) {
return tweets > 2 ? '#d53e4f' :
tweets > 1.5 ? '#f46d43' :
tweets > 1.3 ? '#fdae61' :
tweets > 1.1 ? '#fee08b' :
tweets > 1 ? '#ffffbf' :
tweets > 0.9 ? '#e6f598' :
tweets > 0.5 ? '#abdda4' :
tweets > 0.3 ? '#66c2a5' :
tweets > 0.0 ? '#3288bd' :
tweets == 0 ? '#000' :
'#FFEDA0';
}
function style(feature) {
return {
weight: 2,
opacity: 1,
color: 'white',
dashArray: '3',
fillOpacity: 0.7,
//the colors of the map is based on tweet ratios
fillColor: getColor(feature.properties.tweets)
};
}
function highlightFeature(e) {
var layer = e.target;
layer.setStyle({
weight: 5,
color: '#666',
dashArray: '',
fillOpacity: 0.7
});
if (!L.Browser.ie && !L.Browser.opera) {
layer.bringToFront();
}
info.update(layer.feature.properties);
}
var geojson;
function resetHighlight(e) {
geojson.resetStyle(e.target);
info.update();
}
function zoomToFeature(e) {
map.fitBounds(e.target.getBounds());
}
function onEachFeature(feature, layer) {
layer.on({
mouseover: highlightFeature,
mouseout: resetHighlight,
click: zoomToFeature
});
}
// Change this to pass in data
geojson = L.geoJson(statesData, {
style: style,
onEachFeature: onEachFeature
}).addTo(map);
//Here the source is displayed on the map
map.attributionControl.addAttribution('US state shapes data provided by Mike Bostock <a href="http://bost.ocks.org/mike">D3</a>');
var legend = L.control({position: 'bottomright'});
//This function controls the bottomright legend
legend.onAdd = function (map) {
var div = L.DomUtil.create('div', 'info legend'),
grades = [0, 0.1, 0.3, 0.5, 0.9, 1, 1.1, 1.3, 1.5, 2],
labels = [],
from, to;
for (var i = 0; i < grades.length; i++) {
from = grades[i];
to = grades[i + 1];
if(i === 0) {
labels.push(
'<i style="background:' + getColor(from + 0 ) + '"></i> ' +
'Insuff. Data');
} else if (i === 1) {
labels.push(
'<i style="background:' + getColor(from +.01 ) + '"></i> ' +
'searchTerm2');
} else if (i == 5) {
labels.push(
'<i style="background:' + getColor(from+.01 ) + '"></i> ' +
'Neutral');
}else if (i === grades.length-1) {
labels.push(
'<i style="background:' + getColor(from+.01 ) + '"></i> ' +
'searchTerm1');
}
else{
labels.push(
'<i style="background:' + getColor(from+.01 ) + '"></i> ');
}
}
div.innerHTML = labels.join('<br>');
return div;
};
legend.addTo(map);
</script>
</body>
</html>