-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
364 lines (336 loc) · 17.7 KB
/
index.php
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
<!DOCTYPE html>
<html lang="en" dir="ltr">
<?php
// Include Composer's Autoloader
require 'vendor/autoload.php';
session_start();
$location= '';
$location= $_POST[ 'location' ];
if (!isset($location))
{
$location = $_GET["locName"];
}
$userIP=$_SERVER['REMOTE_ADDR'];
$userLocData = json_decode(file_get_contents("http://ipinfo.io/{$userIP}/json?token=838597e8bcb044"));
$coords = $userLocData->loc;
$coords_explode=explode(",",$coords);
$lat = $coords_explode[0];
$lon = $coords_explode[1];
if (isset($location))
{
//Connection Details
$client = new MongoDB\Client("mongodb://NET302Admin:[email protected]:27017");
$collection = ($client)->NET302DB->$location;
$cursor = $collection->find(
[
],
[
'limit' => 1,
'sort' => [
'_id' => -1,
]
]
);
foreach ($cursor as $Results){
$jsonResults = MongoDB\BSON\toJSON(MongoDB\BSON\fromPHP($Results));
$jsonArray = json_decode($jsonResults, true);
};
}
else {
$location=$userLocData->city;
#setrawcookie("locName", $location, time()+2);
#$_SESSION['locName'] = $location;
header("location:../launchPython.php?locName=$location");
}
$location= strtoupper($location);
############ FUNCTIONS ############
function ordinalSuffix($number) {
$suffix = array('th','st','nd','rd','th','th','th','th','th','th');
if ((($number % 100) >= 11) && (($number%100) <= 13))
return $number. 'th';
else
return $number. $suffix[$number % 10];
}
function day($dayCounter) {
$dateNum = ltrim(date("d", strtotime("+$dayCounter day")), '0');
return date("l", strtotime("+$dayCounter day")) . " " . ordinalSuffix($dateNum);
}
?>
<head>
<!-- Required Metadata -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Import Materialize Framework -->
<link rel="stylesheet" href="css/materialize.min.css">
<!-- Import Google Icons -->
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<!-- Import Custom Styles -->
<link rel="stylesheet" href="css/styles.css">
<nav>
<div class="nav-wrapper" id="main-nav-bar">
<logo href="#" class="brand-logo">WEATHER</logo>
<searchbar>
<form action='../launchPython.php' method='POST'>
<input placeholder="Enter a town or city" name="locName" id="locName" type="textbox" maxlength="120" size="70" required>
<button class="material-icons" id="submitCity" type="submit">search</button>
</form>
</searchbar>
</div>
</nav>
</head>
<body>
<div>
<dt id="currentLocation"><?php echo $location ?></dt>
<div class="row">
<div class="tabUpper">
<div class="tab">
<button class="tablinks active" onclick="openTab(event, 'day1')"><?php echo day(0);?></button>
<button class="tablinks" onclick="openTab(event, 'day2')"><?php echo day(1);?></button>
<button class="tablinks" onclick="openTab(event, 'day3')"><?php echo day(2);?></button>
<button class="tablinks" onclick="openTab(event, 'day4')"><?php echo day(3);?></button>
<button class="tablinks" onclick="openTab(event, 'day5')"><?php echo day(4);?></button>
<button class="tablinks" onclick="openTab(event, 'day6')"><?php echo day(5);?></button>
<button class="tablinks" onclick="openTab(event, 'day7')"><?php echo day(6);?></button>
<button class="tablinks" onclick="openTab(event, 'day8')"><?php echo day(7);?></button>
</div>
</div>
<div id="day1" class="tabcontent">
<img id="day1Icon" src="http://openweathermap.org/img/wn/<?php echo $jsonArray['daily'][0]['weather'][0]['icon'];?>@2x.png">
<dt id="day1Main">Weather: <?php echo ucfirst($jsonArray['daily'][0]['weather'][0]['main']); ?></dt>
<dt id="day1Desc">Description: <?php echo ucfirst($jsonArray['daily'][0]['weather'][0]['description']); ?></dt>
<dt id="day1Temp">Temperature: <?php echo(round($jsonArray['daily'][0]['temp']['min']));?>C - <?php echo(round($jsonArray['daily'][0]['temp']['max']));?> C</dt>
<dt id="day1Humidity">Humidity: <?php echo $jsonArray['daily'][0]['humidity'];?>%</dt>
<dt id="day1WindSpeed">Wind Speed: <?php echo(round($jsonArray['daily'][0]['wind_speed']));?>MpH</dt>
</div>
<div id="day2" class="tabcontent">
<img id="day2Icon" src="http://openweathermap.org/img/wn/<?php echo $jsonArray['daily'][1]['weather'][0]['icon'];?>@2x.png">
<dt id="day2Main">Weather: <?php echo ucfirst($jsonArray['daily'][1]['weather'][0]['main']); ?></dt>
<dt id="day2Desc">Description: <?php echo ucfirst($jsonArray['daily'][1]['weather'][0]['description']); ?></dt>
<dt id="day2Temp">Temperature: <?php echo(round($jsonArray['daily'][1]['temp']['min']));?>C - <?php echo(round($jsonArray['daily'][0]['temp']['max']));?> C</dt>
<dt id="day2Humidity">Humidity: <?php echo $jsonArray['daily'][1]['humidity'];?>%</dt>
<dt id="day2WindSpeed">Wind Speed: <?php echo(round($jsonArray['daily'][1]['wind_speed']));?>MpH</dt>
</div>
<div id="day3" class="tabcontent">
<img id="day3Icon" src="http://openweathermap.org/img/wn/<?php echo $jsonArray['daily'][2]['weather'][0]['icon'];?>@2x.png">
<dt id="day3Main">Weather: <?php echo ucfirst($jsonArray['daily'][2]['weather'][0]['main']); ?></dt>
<dt id="day3Desc">Description: <?php echo ucfirst($jsonArray['daily'][2]['weather'][0]['description']); ?></dt>
<dt id="day3Temp">Temperature: <?php echo(round($jsonArray['daily'][2]['temp']['min']));?>C - <?php echo(round($jsonArray['daily'][0]['temp']['max']));?> C</dt>
<dt id="day3Humidity">Humidity: <?php echo $jsonArray['daily'][2]['humidity'];?>%</dt>
<dt id="day3WindSpeed">Wind Speed: <?php echo(round($jsonArray['daily'][2]['wind_speed']));?>MpH</dt>
</div>
<div id="day4" class="tabcontent">
<img id="day4Icon" src="http://openweathermap.org/img/wn/<?php echo $jsonArray['daily'][3]['weather'][0]['icon'];?>@2x.png">
<dt id="day4Main">Weather: <?php echo ucfirst($jsonArray['daily'][3]['weather'][0]['main']); ?></dt>
<dt id="day4Desc">Description: <?php echo ucfirst($jsonArray['daily'][3]['weather'][0]['description']); ?></dt>
<dt id="day4Temp">Temperature: <?php echo(round($jsonArray['daily'][3]['temp']['min']));?>C - <?php echo(round($jsonArray['daily'][0]['temp']['max']));?> C</dt>
<dt id="day4Humidity">Humidity: <?php echo $jsonArray['daily'][3]['humidity'];?>%</dt>
<dt id="day4WindSpeed">Wind Speed: <?php echo(round($jsonArray['daily'][3]['wind_speed']));?>MpH</dt>
</div>
<div id="day5" class="tabcontent">
<img id="day5Icon" src="http://openweathermap.org/img/wn/<?php echo $jsonArray['daily'][4]['weather'][0]['icon'];?>@2x.png">
<dt id="day5Main">Weather: <?php echo ucfirst($jsonArray['daily'][4]['weather'][0]['main']); ?></dt>
<dt id="day5Desc">Description: <?php echo ucfirst($jsonArray['daily'][4]['weather'][0]['description']); ?></dt>
<dt id="day5Temp">Temperature: <?php echo(round($jsonArray['daily'][4]['temp']['min']));?>C - <?php echo(round($jsonArray['daily'][0]['temp']['max']));?> C</dt>
<dt id="day5Humidity">Humidity: <?php echo $jsonArray['daily'][4]['humidity'];?>%</dt>
<dt id="day5WindSpeed">Wind Speed: <?php echo(round($jsonArray['daily'][4]['wind_speed']));?>MpH</dt>
</div>
<div id="day6" class="tabcontent">
<img id="day6Icon" src="http://openweathermap.org/img/wn/<?php echo $jsonArray['daily'][5]['weather'][0]['icon'];?>@2x.png">
<dt id="day6Main">Weather: <?php echo ucfirst($jsonArray['daily'][5]['weather'][0]['main']); ?></dt>
<dt id="day6Desc">Description: <?php echo ucfirst($jsonArray['daily'][5]['weather'][0]['description']); ?></dt>
<dt id="day6Temp">Temperature: <?php echo(round($jsonArray['daily'][5]['temp']['min']));?>C - <?php echo(round($jsonArray['daily'][0]['temp']['max']));?> C</dt>
<dt id="day6Humidity">Humidity: <?php echo $jsonArray['daily'][5]['humidity'];?>%</dt>
<dt id="day6WindSpeed">Wind Speed: <?php echo(round($jsonArray['daily'][5]['wind_speed']));?>MpH</dt>
</div>
<div id="day7" class="tabcontent">
<img id="day7Icon" src="http://openweathermap.org/img/wn/<?php echo $jsonArray['daily'][6]['weather'][0]['icon'];?>@2x.png">
<dt id="day7Main">Weather: <?php echo ucfirst($jsonArray['daily'][6]['weather'][0]['main']); ?></dt>
<dt id="day7Desc">Description: <?php echo ucfirst($jsonArray['daily'][6]['weather'][0]['description']); ?></dt>
<dt id="day7Temp">Temperature: <?php echo(round($jsonArray['daily'][6]['temp']['min']));?>C - <?php echo(round($jsonArray['daily'][0]['temp']['max']));?> C</dt>
<dt id="day7Humidity">Humidity: <?php echo $jsonArray['daily'][6]['humidity'];?>%</dt>
<dt id="day7WindSpeed">Wind Speed: <?php echo(round($jsonArray['daily'][6]['wind_speed']));?>MpH</dt>
</div>
<div id="day8" class="tabcontent">
<img id="day8Icon" src="http://openweathermap.org/img/wn/<?php echo $jsonArray['daily'][7]['weather'][0]['icon'];?>@2x.png">
<dt id="day8Main">Weather: <?php echo ucfirst($jsonArray['daily'][7]['weather'][0]['main']); ?></dt>
<dt id="day8Desc">Description: <?php echo ucfirst($jsonArray['daily'][7]['weather'][0]['description']); ?></dt>
<dt id="day8Temp">Temperature: <?php echo(round($jsonArray['daily'][7]['temp']['min']));?>C - <?php echo(round($jsonArray['daily'][0]['temp']['max']));?> C</dt>
<dt id="day8Humidity">Humidity: <?php echo $jsonArray['daily'][7]['humidity'];?>%</dt>
<dt id="day8WindSpeed">Wind Speed: <?php echo(round($jsonArray['daily'][7]['wind_speed']));?>MpH</dt>
</div>
</div>
</div>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js">
</script>
<script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCYqRjg-B_g9VkcS6P2yMTflcknwaZAxzo&callback=initMap" type="text/javascript"></script>
<script>
var map;
var geoJSON;
var request;
var gettingData = false;
var openWeatherMapKey = "a1772b5eb4516dca799fa31e7482ac70"
function initialize() {
var mapOptions = {
zoom: 11,
center: new google.maps.LatLng("<?php echo $jsonArray['lat'];?>","<?php echo $jsonArray['lon'];?>")
};
map = new google.maps.Map(document.getElementById('map-canvas'),mapOptions);
// Add interaction listeners to make weather requests
google.maps.event.addListener(map, 'idle', checkIfDataRequested);
// Sets up and populates the info window with details
map.data.addListener('click', function(event) {
infowindow.setContent(
"<img src=" + event.feature.getProperty("icon") + ">"
+ "<br /><strong>" + event.feature.getProperty("city") + "</strong>"
+ "<br />" + event.feature.getProperty("temperature") + "°C"
+ "<br />" + event.feature.getProperty("weather")
);
infowindow.setOptions({
position:{
lat: event.latLng.lat(),
lng: event.latLng.lng()
},
pixelOffset: {
width: 0,
height: -15
}
});
infowindow.open(map);
});
}
var checkIfDataRequested = function() {
// Stop extra requests being sent
while (gettingData === true) {
request.abort();
gettingData = false;
}
getCoords();
};
// Get the coordinates from the Map bounds
var getCoords = function() {
var bounds = map.getBounds();
var NE = bounds.getNorthEast();
var SW = bounds.getSouthWest();
getWeather(NE.lat(), NE.lng(), SW.lat(), SW.lng());
};
// Make the weather request
var getWeather = function(northLat, eastLng, southLat, westLng) {
gettingData = true;
var requestString = "http://api.openweathermap.org/data/2.5/box/city?bbox="
+ westLng + "," + northLat + "," //left top
+ eastLng + "," + southLat + "," //right bottom
+ map.getZoom()
+ "&cluster=yes&format=json"
+ "&APPID=" + openWeatherMapKey;
request = new XMLHttpRequest();
request.onload = proccessResults;
request.open("get", requestString, true);
request.send();
};
// Take the JSON results and proccess them
var proccessResults = function() {
console.log(this);
var results = JSON.parse(this.responseText);
if (results.list.length > 0) {
resetData();
for (var i = 0; i < results.list.length; i++) {
geoJSON.features.push(jsonToGeoJson(results.list[i]));
}
drawIcons(geoJSON);
}
};
var infowindow = new google.maps.InfoWindow();
// For each result that comes back, convert the data to geoJSON
var jsonToGeoJson = function (weatherItem) {
var feature = {
type: "Feature",
properties: {
city: weatherItem.name,
weather: weatherItem.weather[0].main,
temperature: weatherItem.main.temp,
min: weatherItem.main.temp_min,
max: weatherItem.main.temp_max,
humidity: weatherItem.main.humidity,
pressure: weatherItem.main.pressure,
windSpeed: weatherItem.wind.speed,
windDegrees: weatherItem.wind.deg,
windGust: weatherItem.wind.gust,
icon: "http://openweathermap.org/img/w/"
+ weatherItem.weather[0].icon + ".png",
coordinates: [weatherItem.coord.Lon, weatherItem.coord.Lat]
},
geometry: {
type: "Point",
coordinates: [weatherItem.coord.Lon, weatherItem.coord.Lat]
}
};
// Set the custom marker icon
map.data.setStyle(function(feature) {
return {
icon: {
url: feature.getProperty('icon'),
anchor: new google.maps.Point(25, 25)
}
};
});
// returns object
return feature;
};
// Add the markers to the map
var drawIcons = function (weather) {
map.data.addGeoJson(geoJSON);
// Set the flag to finished
gettingData = false;
};
// Clear data layer and geoJSON
var resetData = function () {
geoJSON = {
type: "FeatureCollection",
features: []
};
map.data.forEach(function(feature) {
map.data.remove(feature);
});
};
google.maps.event.addDomListener(window, 'load', initialize);
</script>
<div id="map-canvas"></div>
</body>
<footer class="page-footer" id="footer">
<div class="container">
<div class="row">
<div class="col l6 s12">
<p class="grey-text text-lighten-4">"WS2"</p>
</div>
</div>
</div>
<div class="footer-copyright">
<div class="container">
© 2021 William Tyler (NET302_Project)
</div>
</div>
</footer>
<!-- JAVA SCRIPT -->
<!-- Import Frameworks -->
<script type="text/javascript" src="../js/jquery.js"></script>
<script type="text/javascript" src="../js/materialize.min.js"></script>
<!-- Import Common Code -->
<!-- <script type="text/javascript" src="js/functions.js"></script> -->
<!-- <script type="text/javascript" src="js/global.js"></script> -->
<!-- Page Specific Script -->
<script>
function openTab(evt, tabName) {
var i, tabcontent, tablinks;
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active", "");
}
document.getElementById(tabName).style.display = "block";
evt.currentTarget.className += " active";
}
</script>
</html>