-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweather_map.html
More file actions
312 lines (283 loc) · 12 KB
/
weather_map.html
File metadata and controls
312 lines (283 loc) · 12 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>OpenWeather API</title>
<link href='https://api.mapbox.com/mapbox-gl-js/v2.2.0/mapbox-gl.css' rel='stylesheet'/>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-wEmeIV1mKuiNpC+IOBjI7aAzPcEZeedi5yW5f2yOq55WWLwNGmvvx4Um1vskeMj0" crossorigin="anonymous">
<script src="https://kit.fontawesome.com/82e38e0b18.js" crossorigin="anonymous"></script>
<style>
.mainContainer{
display: flex;
justify-content: center;
width: 90%;
margin: auto;
}
.container {
border: 1px solid #e0e0e0;
border-radius: 5px;
display: flex;
align-items: center;
flex-direction: column;
margin: 50px;
width: 20%;
background-color: #efefef;
}
.searchContainer, #toggleCards{
margin: 50px 10% 0 10%;
display: flex;
flex-direction: row;
justify-content: space-between;
}
#toggleCards{
margin-top: 2px;
}
h1 {
margin-top: 20px;
border: 1px solid grey;
padding: 50px;
border-radius: 50%;
background-color: whitesmoke;
}
.headerContainer{
width: calc(100% + 24px);
background-color: #d9d9d9;
}
h4{
text-align: center;
margin: 10px 0;
}
.riseSet{
width: 100%;
margin: 10px 0 -10px 0;
padding: 10px;
border-top: 1px solid black;
}
i{
color: #ff8c59;
margin: 0 44%;
}
p{
font-weight: bold;
}
#moon{
color: #ffffff;
}
.hiddenTest{
display: none;
}
.grow{
transition: all .2s ease-in-out;
}
.grow:hover{
transform: scale(1.1);
}
#map{
margin-top: 20px;
border: 3px solid grey;
border-radius: 1em;
}
button, input, select{
padding: 6px;
border: 1px solid lightgrey;
border-radius: 6px;
margin-bottom: 2px;
}
.onBottom{
margin-bottom: 10px;
}
</style>
</head>
<body>
<!--Move everything to their own page-->
<!--Can I toggle units?-->
<div class="searchContainer">
<div>
<input type="text" id="user-input" placeholder="Ex: Dallas, Texas">
<button id="search" type="button">Search!</button>
</div>
<select id="mapStyle">
<option value="mapbox://styles/mapbox/streets-v11">Streets v11</option>
<option value="mapbox://styles/mapbox/outdoors-v11">Outdoor v11</option>
<option value="mapbox://styles/mapbox/light-v10">Light v10</option>
<option value="mapbox://styles/mapbox/dark-v10">Dark v10</option>
<option value="mapbox://styles/mapbox/satellite-v9">Satellite v9</option>
<option value="mapbox://styles/mapbox/satellite-streets-v11">Satellite Streets v11</option>
<option value="mapbox://styles/mapbox/navigation-preview-day-v4">Daytime preview</option>
<option value="mapbox://styles/mapbox/navigation-preview-night-v4">Nighttime preview</option>
<option value="mapbox://styles/mapbox/navigation-guidance-day-v4">Navigational Day</option>
<option value="mapbox://styles/mapbox/navigation-guidance-night-v4">Navigational Night</option>
</select>
</div>
<div id='map' style='width: 80vw; height: 80vh; margin: auto;'></div>
<button id="toggleCards">Show all days</button>
<div class="mainContainer">
<div id="insertInfo" class="d-flex row justify-content-evenly"></div>
</div>
<script src="js/jquery-3.6.0.min.js"></script>
<script src="js/keys.js"></script>
<script src='https://api.mapbox.com/mapbox-gl-js/v2.2.0/mapbox-gl.js'></script>
<script src="js/mapbox-geocoder-utils.js"></script>
<script>
//create map
mapboxgl.accessToken = MAPBOX_ACCESS_TOKEN;
var map = new mapboxgl.Map({
container: 'map', // container ID
style: 'mapbox://styles/mapbox/streets-v11', // style URL
center: [-97.109, 32.738], // starting position [lng, lat]
zoom: 10 // starting zoom
});
var marker = new mapboxgl.Marker({
draggable: true,
color: "red",
})
.setLngLat([-97.109, 32.738])
.addTo(map);
//set coordinates to use in the "write" function
var coordinates = [marker.getLngLat().lat, marker.getLngLat().lng];
marker.on("dragend", function () {
coordinates = [marker.getLngLat().lat, marker.getLngLat().lng]
console.log(coordinates)
write();
});
//when the user double clicks on the map, move the cursor and post that information.
map.on("dblclick", listener => {
marker.setLngLat(listener.lngLat);
map.flyTo({
center: [listener.lngLat.lng, listener.lngLat.lat],
speed: 1,
zoom: 10,
});
coordinates = [listener.lngLat.lat, listener.lngLat.lng]
write();
});
//Button calls the "moveMarker" function and uses geocode to turn text input into Longitude and Latitudinal coordinates.
$("#search").click(function(){
userInput = $("#user-input").val();
console.log(userInput);
moveMarkerToInput()
})
$("#mapStyle").change(function(){
map.setStyle($("#mapStyle").val())
})
//The only reason anything is on screen
function write() {
$.ajax("https://api.openweathermap.org/data/2.5/onecall?units=imperial&lat=" + coordinates[0] + "&lon=" + coordinates[1] + "&exclude=minutely,hourly&appid=" + WEATHERMAP_ACCESS_TOKEN)
.done(function (response) {
$("#insertInfo").html("");
//iterate with response.daily.length
for (var i = 0; i < response.daily.length; i++) {
var today = response.daily[i];
var todayDate = new Date(today.dt * 1000);
todayDate = todayDate.toDateString()
//Create the cards: jumbled mess of JS HTML
var html = ""
html = "<div class='container grow col-sm weather-" + (i + 1) + "'>";
html += "<div class='headerContainer'><h4>" + todayDate + "</h4></div>"
//Get current weather if it's "today"
if(i === 0) {
html += "<h1 style='background-image: url(http://openweathermap.org/img/w/" + today.weather[0].icon + ".png); background-repeat: no-repeat; background-position: top;'>" + Math.round(response.current.temp) + "°</h1>";
}else{
html += "<h1 style='background-image: url(http://openweathermap.org/img/w/" + today.weather[0].icon + ".png); background-repeat: no-repeat; background-position: top;'>" + Math.round(today.temp.day) + "°</h1>";
}
html += "<p>Real Feel: " + Math.round(today.feels_like.day) + "°</p>";
html += "<div> low: " + Math.round(today.temp.min) + "° / High: " + Math.round(today.temp.max) + "°</div>";
html += "<div> Humidity: " + today.humidity + "%</div>";
html += "<div class='onBottom'> Chance of rain: " + Math.round(today.pop * 100) + "%</div>";
html += "<div class='hiddenTillClick hiddenTest'>";
//wind speed/direction and gusts
html += "<div style='border-bottom: 1px solid black; width: 100%;'><p style='margin: 0 35%;'>Wind</p></div>";
html += "<div style='text-align: center'>";
html += "<div>" + displayWindDirection(today.wind_deg) + " " + Math.round(today.wind_speed) + "MPH</div>";
html += "<div>Gusts: " + Math.round(today.wind_gust) + "MPH</div></div>";
//sunrise
html += "<div class='riseSet'><i class=\"fas fa-sun\"></i></div>";
html += "<div class='row'>";
html += "<div class='col'>Rise:<p>"+ riseSet(today.sunrise) + "am</p></div>";
html += "<div class='col'>Set:<p>" + riseSet(today.sunset) + "pm</p></div></div>";
//moonrise
html += "<div class='riseSet'><i id='moon' class=\"fas fa-moon\"></i></div>";
html += "<div class='row'>";
html += "<div class='col'>Rise:<p>"+ riseSet(today.moonset) + "pm</p></div>";
html += "<div class='col'>Set:<p>" + riseSet(today.moonrise) + "am</p></div></div></div>";
html += "</div>";
$("#insertInfo").append(html);
//hide the second half of the card till clicked
$(".weather-"+ (i + 1) + "").click(function(){
$(".hiddenTillClick").slideToggle(1000);
})
//set 5-8 to hide
$(".weather-5, .weather-6, .weather-7, .weather-8").addClass("hiddenTest");
}
console.log(response);
});
}
write();
//click to enable/disable cards
$("#toggleCards").click(toggle);
//===============Extra Functions=============//
//Find the cardinal direction of the wind using the angle given by API
function displayWindDirection(windDirection){
var cardinalDirection = "";
if(windDirection >= 349 || windDirection <= 11){
cardinalDirection = "N";
}else if(windDirection >= 12 && windDirection <= 34){
cardinalDirection = "NNE";
}else if(windDirection >= 35 && windDirection <= 56){
cardinalDirection = "NE";
}else if(windDirection >= 57 && windDirection <= 79){
cardinalDirection = "ENE";
}else if(windDirection >= 80 && windDirection <= 101){
cardinalDirection = "E";
}else if(windDirection >= 102 && windDirection <= 124){
cardinalDirection = "ESE";
}else if(windDirection >= 125 && windDirection <= 146){
cardinalDirection = "SE";
}else if(windDirection >= 147 && windDirection <= 169){
cardinalDirection = "SSE";
}else if(windDirection >= 170 && windDirection <= 191){
cardinalDirection = "S";
}else if(windDirection >= 192 && windDirection <= 214){
cardinalDirection = "SSW";
}else if(windDirection >= 215 && windDirection <= 236){
cardinalDirection = "SW";
}else if(windDirection >= 237 && windDirection <= 258){
cardinalDirection = "WSW";
}else if(windDirection >= 259 && windDirection <= 281){
cardinalDirection = "W";
}else if(windDirection >= 282 && windDirection <= 303){
cardinalDirection = "WNW";
}else if(windDirection >= 304 && windDirection <= 326){
cardinalDirection = "NW";
}else if(windDirection >= 327 && windDirection <= 348){
cardinalDirection = "NNW";
}else{
cardinalDirection = "Error";
}
return cardinalDirection;
}
function moveMarkerToInput(){
geocode(userInput, MAPBOX_ACCESS_TOKEN).then(function (results){
marker.setLngLat(results);
coordinates = [marker.getLngLat().lat, marker.getLngLat().lng];
map.flyTo({
center: results,
zoom: 10,
speed: 1
});
write();
});
}
//toggle the last 4 to hide or show
function toggle() {
$(".weather-5, .weather-6, .weather-7, .weather-8").toggleClass("hiddenTest");
}
//get the sun(moon)rise and sun(moon)set as readable variables
function riseSet(item){
var readableItem = new Date(item * 1000);
readableItem = readableItem.getHours() + ":" + readableItem.getMinutes();
return readableItem;
}
</script>
</body>
</html>