-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmapping.php
More file actions
371 lines (276 loc) · 9.2 KB
/
mapping.php
File metadata and controls
371 lines (276 loc) · 9.2 KB
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
<?php
// testing code
print_r($_POST);
/*
* Modifications
*
* 6/4/2016
*
* had problems using jQuery JSON parsing
* used javascript JSON parse
* stringify to convert to a string - store in local storage
* get from local storage - JSON parse
*
* finally made markers appear in Google Maps
*
*
* 6/4/2016
* -loop through locations, getting their attribs and putting into markers
* -added infoWindow for markers, displaying HTML containing keyword count
* (and possibly some other useful data)
*
* TODO:
* -put keyword searched in the infoWindow
* (and data collected from Python scraper)
*
* -integrity of results.json file
* correct json output from python->php->js
* make sure it follows [{},{},{}]
* include all results now (all areas)
*
* PYTHON
* -correct JSON output (or do it in PHP)
* -might have to re run python scraper again
* (note application of Google geolocation API keys )
*
*
*/
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Map result</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="css/salamander.css">
</head>
<!-- ONLOAD, call this function -->
<!-- body onload = "getResults()" -->
<body>
<!---- AJAX ------------->
<!--script type="text/javascript" src= "ajax_read_jsonresult.js" async>
</script-->
<!------------------------->
<!-- directly embedded getResults() AJAX function instead -->
<script>
function getResults() {
//document.write('hello');
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
//## xhttp.responseText holds the server responseText ##
//document.getElementById("data").innerHTML = xhttp.responseText;
jsonData = xhttp.responseText;
document.getElementById("data").innerHTML = jsonData;
//SHOULD SEE EVERYTING HERE - TESTING
//----> console.log(jsonData);
//JSON data includes a []
/*
1. decode json - can probably use a function
2. put it in array
3. put Google Maps script here, easier to load vars from json to google markers
-coz were in Javascript!!!!
*/
//console.log(typeof(jsonData));
//obj = JSON.parse(jsonData);
//convert to a string to be able to store in HTML localStorage
console.log(jsonData);
obj = JSON.stringify(jsonData);
localStorage.setItem("searchResults", obj);
//console.log(obj);
//console.log(obj.length);
//console.log(parseJSON($jsonData));
/*
htmlString = "<table border='1'>";
for (x=0; x < objCount; x++)
{
htmlString += "<tr><td>" + obj[x].Area + "</td><td>" + obj[x].Count + "</td><td>" + obj[x].Lat + "</td><td>" + obj[x].Long + "</td><td>" + obj[x].Name + "</td></tr>" ;
}
htmlString += "</table>";
//document.getElementById("scratch").innerHTML = "=====================================" + htmlString;
*/
//console.log(obj.toString());
}
};
xhttp.open("GET", "results.json", true);
xhttp.send();
} //end getResults() function
// could also call getResults() from here
// getResults()
//document.write(getResults());
</script>
<div id="results"></div>
<div id="googleMap" style="height:400px;width:100%;"></div>
<!-- load data here -->
<div id="data"></div>
<!-- scratch -->
<div id="scratch"></div>
<div id="maps">
<!-- ############## MAPS ########################## -->
<!-- Add Google Maps -->
<script src="http://maps.googleapis.com/maps/api/js"></script>
<script>
// call function
getResults();
// get HTML5 storage item stored
var dataStored = JSON.parse(localStorage.getItem("searchResults"));
//console.log(dataStored);
//window.alert(dataStored);
//console.log(dataStored.length);
//console.log(typeof(dataStored));
// parse to an object array
datas = JSON.parse(dataStored);
//console.log(datas);
//console.log(datas.posts[0]);
//console.log(datas.posts[1]);
//console.log(datas.posts[1] == null);
//console.log(datas.keyword);
//console.log("count" + datas.posts.length);
//console.log(datas.posts[0].Area);
//console.log("length:" + datas.posts.length);
//print HTML table of obtained location tuples
htmlString = "<table border='1'>";
//for (x=0; x < datas.length; x++)
for (x=0; x < datas.posts.length; x++)
{
//htmlString += "<tr><td>" + datas[x].Area + "</td><td>" + datas[x].Count + "</td><td>" + datas[x].Lat + "</td><td>" + datas[x].Long + "</td><td>" + datas[x].Name + "</td></tr>" ;
//console.log( datas.posts[x].Area);
//console.log( datas.posts[x].Count);
//console.log( datas.posts[x].Lat);
//console.log( datas.posts[x].Long);
//console.log( datas.posts[x].Name);
//console.log(datas.posts[x].Area == null);
if (datas.posts[x] == null)
{
//console.log("null object");
}
else
{
htmlString += "<tr><td>" + datas.posts[x].Area + "</td><td>" + datas.posts[x].Count + "</td><td>" + datas.posts[x].Lat + "</td><td>" + datas.posts[x].Long + "</td><td>" + datas.posts[x].Name + "</td></tr>" ;
}
}
htmlString += "</table>";
//console.log(JSON.parse(dataStored));
document.getElementById("scratch").innerHTML = "=====================================" + htmlString;
//console.log(htmlString);
//use center location as the center
var myCenter = new google.maps.LatLng(45.52605, -73.59505);
//https://developers.google.com/maps/documentation/javascript/examples/marker-labels
//initialize google maps
function initialize() {
// set up map, scrollable and draggable
var mapProp = {
center: myCenter,
zoom:12,
scrollwheel:true,
draggable:true,
mapTypeId:google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("googleMap"),mapProp);
//###############################################
//setup one marker at center
var marker = new google.maps.Marker({
position:myCenter
});
marker.setMap(map);
//#############################################
var markers = [];
// loop through locations, get their Lat ang Long then make markers for each
for (x=0; x < datas.posts.length; x++)
{
if (datas.posts[x] == null)
{
//console.log("FUCK NULL" + x);
}
else
{
//console.log(datas.posts[x]);
//customize label based on number of keywords
//unfortunately, only 1 char is allowed to display in marker label
if (datas.posts[x].Count == 0)
label = '0' ;
else
label = '☺';
// new marker
var myLatLng2 = {lat: datas.posts[x].Lat, lng: datas.posts[x].Long};
var marker2 = new google.maps.Marker({
position: myLatLng2,
map: map,
title: datas.posts[x].Name,
label: label,
html: '<div>Keyword count:' + datas.posts[x].Count + '<br />Area: ' + datas.posts[x].Area + '<br />' + datas.posts[x].Name + '<br />Accidents:' + datas.posts[x].Accidents + '<br />Temperature:' + datas.posts[x].Weather + '</div>'
});
//add to markers array
markers.push(marker2);
//string for info window
var infowindow = new google.maps.InfoWindow({
content: "hehe"
});
console.log(markers[x]);
/*
//BUGGY: only applies html to last one
markers[x].addListener('click', function() {
infowindow.setContent(markers[x].html);
infowindow.open(map, markers[x]);
});
*/
//this one works - puts a listener to this current marker
google.maps.event.addListener(marker2,'click', function() {
infowindow.setContent(this.html);
infowindow.open(map, this);
});
//same as setting the map attrib in marker def
//marker2.setMap(map);
}
} // #END FOR
} //end of GoogleMaps init()
google.maps.event.addDomListener(window, 'load', initialize);
</script>
<!------------------------------------------------->
<script>
$(document).ready(function(){
// Add smooth scrolling to all links in navbar + footer link
$(".navbar a, footer a[href='#myPage']").on('click', function(event) {
// Prevent default anchor click behavior
event.preventDefault();
// Store hash
var hash = this.hash;
// Using jQuery's animate() method to add smooth page scroll
// The optional number (900) specifies the number of milliseconds it takes to scroll to the specified area
$('html, body').animate({
scrollTop: $(hash).offset().top
}, 900, function(){
// Add hash (#) to URL when done scrolling (default click behavior)
window.location.hash = hash;
});
});
})
$(window).scroll(function() {
$(".slideanim").each(function(){
var pos = $(this).offset().top;
var winTop = $(window).scrollTop();
if (pos < winTop + 600) {
$(this).addClass("slide");
}
});
});
</script>
</div>
<!-- jquery script, upod loading of data to "scratch" div, execute this -->
<script>
/*
$( "#scratch" ).click(function() {
var htmlString = $( this ).html();
$( this ).text( htmlString );
});
*/
$( "#scratch" ).click(function() {
var htmlString = $( this ).html();
$( this ).text( htmlString );
});
</script>
</body>
</html>