-
Notifications
You must be signed in to change notification settings - Fork 0
/
tweetmap.html
354 lines (276 loc) · 11.4 KB
/
tweetmap.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
</head>
<body>
<div class="container">
<!-- Fixed navbar -->
<nav class="navbar navbar-expand-md navbar-dark fixed-top bg-dark">
<p class="navbar-brand" style=""> Tweet Map</p>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
</nav>
<h1> <br /><br /> </h1>
<!-- Row for the select form -->
<div class="row">
<div class="col-md-3">
<div class="form-group">
<form method="post" id="myform" name="myform">
<select class="form-control" id="dropdown-menu" name="dropdown-menu">
<option value="">Select the Topic</option>
<option value="Trump"> Trump </option>
<option value="Modi"> Modi</option>
<option value="Google">Google</option>
<option value="Apple">Apple</option>
<option value="Android">Andriod</option>
<option value="iOS">iOS</option>
<option value="Hillary">Hillary</option>
<option value="Hacker">Hacker</option>
</select>
</form>
</div>
</div>
<div class="col-md-6 content">
<div id="status"></div>
</div>
</div>
<!-- start map -->
<div id="map" style="width:1050px;height:500px;background:blue;">
</div>
<!-- end map -->
<!-- start modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header" style = "background-color: #2a4d6b;">
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<div class="modal-body">
<p id="modal-message"> Hii</p>
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<!-- end modal -->
</div>
<!-- end container -->
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js" integrity="sha384-b/U6ypiBEHpOf/4+1nzFpr53nxSS+GLCkfwBdFNTxtclqqenISfwAzpKaMNFNmj4" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js" integrity="sha384-h0AbiXch4ZDo7tp9hKZ4TsHbi047NrKGLO3SEJAg45jXxnGIfYzk4Si90RDIqNm1" crossorigin="anonymous"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/JavaScript">
// Declaring Global variables
var plotDict = [];
var map;
var markers = [];
var geocoder;
var resulObj = null;
var image = "https://maps.gstatic.com/intl/en_ALL/mapfiles/markers2/measle.png";
// This will be triggered on change event of dropdown-menu
// Form will be submited
// var requestInterval = setInterval(postData, 10000);
$(document).ready(function() {
$('#dropdown-menu').on('change', function() {
deleteMarkers();
postData();
//document.forms['myform'].submit();
});
});
function postData(){
$('#status').text('Waiting for Live Tweets');
var search = $("#dropdown-menu").val();
if(search != null && search != undefined){
var data = { 'search': search }
var hits_counter = 0;
$.ajax({
type : 'POST',
dataType: 'json',
crossDomain: true,
url: 'https://44l1kb69p1.execute-api.us-east-2.amazonaws.com/prod/helloworld',
data: JSON.stringify(data),
success: function (data) {
$('#status').text('');
console.log("succes");
console.log(data);
if(data.status == 'true'){
function myElasticRequest(){
$('#status').text('Waiting for Live Tweets');
var search = $("#dropdown-menu").val();
if(search != null && search != undefined){
e_url = 'https://search-tweetsentiment-qgnbjonsbxhe6v4btn5chvrpgy.us-east-2.es.amazonaws.com/tweet_sentiment/_search?q='+search;
//This will be the request to be sent to ElasticSearch
$.ajax({
type : 'GET',
crossDomain: true,
contentType: 'application/json',
url: e_url,
success: function (response) {
// Updating hits counter and hits occured
var hits = response['hits']['total']
var new_hits = hits - hits_counter;
hits_counter = hits;
if(hits != 0){
$('#status').text('New Tweets: '+ new_hits);
var elastic_data = response['hits']['hits'];
//console.log(elastic_data);
var tweet_list = [];
for (i = 0 ; i < hits; i++) {
//console.log(elastic_data[0]);
data = elastic_data[i];
tweet_list.push(data['_source']);
//console.log("The tweet list: "+ tweet_list);
}
deleteMarkers();
//console.log(" "+data);
//console.log(response);
plotData(tweet_list);
}
},
error: function() {
console.log("Elasticsearch Failure");
},
complete: function() {
// Schedule the next request when the current one's complete
setTimeout(myElasticRequest, 5000);
}
});
}
}
// Requesting above function
myElasticRequest();
}
else{
$('#status').text('Sorry No Live Tweets available');
}
},
error: function() {
console.log("failure");
},
complete: function() {
// Schedule the next request when the current one's complete
setTimeout(postData, 50000);
}
});
}
}
// Function for initialization of Google MAP
function myMap() {
// Image for the pushpin Object
var image;
var mapCanvas = document.getElementById("map");
var mapOptions = {
center: new google.maps.LatLng(37.09024, -95.712891),
disableDefaultUI: true,
zoom: 2 ,
styles: [
{elementType: 'geometry', stylers: [{color: '#242f3e'}]},
{elementType: 'labels.text.stroke', stylers: [{color: '#242f3e'}]},
{elementType: 'labels.text.fill', stylers: [{color: '#746855'}]},
{
featureType: 'water',
elementType: 'geometry',
stylers: [{color: '#17263c'}]
},
{
featureType: 'poi',
elementType: 'labels.text.fill',
stylers: [{color: '#d59563'}]
},
{
featureType: 'water',
elementType: 'labels.text.fill',
stylers: [{color: '#515c6d'}]
},
{
featureType: 'water',
elementType: 'labels.text.stroke',
stylers: [{color: '#17263c'}]
}
],
};
//geocoder = new google.maps.Geocoder();
map = new google.maps.Map(mapCanvas, mapOptions);
}
// This functions is used to plot the marker on google map. It returns the marker object
function plotMarker(tweet, map){
// Setting the marker object with the map, latitude and longitude data
var marker = new google.maps.Marker({
position: {lat: parseFloat(tweet.latitude), lng: parseFloat(tweet.longitude)},
map: map,
icon: image
});
markers.push(marker);
var infowindow = new google.maps.InfoWindow({
content : tweet.text
});
marker.addListener('mouseover', function(){
infowindow.open(map, marker)
})
marker.addListener('mouseout', function(){
infowindow.close()
})
}
function deleteMarkers(){
// Refreshing the entire map for deleteMarkers
myMap();
}
function plotData(tweets){
// Storing the tweetData.tweet
var tweetsArray = tweets;
console.log(tweetsArray);
// If there are tweets for the given topic Iterate
if(tweetsArray.length > 0){
// Creating marker and InfoWindow for the Google Map Objects
var marker = null, i;
var infowindow = new google.maps.InfoWindow();
console.log("Tweets array is there sa.");
// Iterating over the tweetsArray
for(i = 0; i<tweetsArray.length; i++){
// Getting the Tweet data
var tweet = tweetsArray[i];
if(tweet.hasOwnProperty('latitude')){
if(tweet.latitude){
if(tweet.sentiment_label == "positive"){
// Setting the image URL to measle_blue;
image= "https://maps.gstatic.com/intl/en_us/mapfiles/markers2/measle_blue.png";
}
else if(tweet.sentiment_label == "negative"){
image = "https://storage.googleapis.com/support-kms-prod/SNP_2752125_en_v0";
}
else if(tweet.sentiment_label == "neutral"){
// Setting the image URL to measle_blue;
image= "https://maps.gstatic.com/intl/en_us/mapfiles/markers2/measle_blue.png";
}
}
else if(tweet.latitude == 'None' || tweet.longitude == 'None'){
}
// Plotting on map and adding marker and create infowindow
plotMarker(tweet, map);
markers[i].setMap(map);
}
}
$('#status').text('No of Tweets Returned '+ tweetsArray.length);
console.log(tweetsArray.length);
}
else{
// No Live Tweets for this topic
$('#status').text('Sorry No Live Tweets available');
}
}
$('.dropdown-menu').on('click', 'a', function(e){
// 'this' is the clicked anchor
var text = this.text;
var href = this.href;
alert(text);
// here add to local storage;
});
</script>
<!-- Google Map API call -->
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAIWnwGb9WRladk_47LmMTH6cte9eC6Ob4&callback=myMap"></script>
</body>
</html>