-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
265 lines (188 loc) · 11.1 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Re:Fresh Forecast</title>
<!-- CSS for Boostrap, Mapbox 1.8.0, Mapbox geocoder, and Google fonts import -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous" />
<link href='https://api.mapbox.com/mapbox-gl-js/v1.8.0/mapbox-gl.css' rel='stylesheet' />
<link rel="stylesheet" href="https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-geocoder/v4.4.2/mapbox-gl-geocoder.css" type="text/css" />
<link href="https://fonts.googleapis.com/css?family=Dosis&display=swap" rel="stylesheet">
<!-- Custom CSS -->
<link rel="stylesheet" href="css/weather_map.css">
</head>
<!-- ******** HTML ******** -->
<body>
<!-- Displays City/general area, date, and current temperature of specified location -->
<div id="current">
<div id="currentLocation">San Antonio</div>
<div id="currentDate"></div>
<div id="currentTemp"></div>
</div>
<main>
<!-- Generates cards to left w/ loading icons -->
<div id="threeDayForecast">
<div class="card p-3 mb-3" style="height:206px;border-radius:20px;margin-top:0;"><img class="loadingIcon" src="img/icons/loading/Ellipsis-1.3s-200px.svg"></div>
<div class="card p-3 mb-3" style="height:206px;border-radius:20px;margin-top:0;"><img class="loadingIcon" src="img/icons/loading/Ellipsis-1.3s-200px.svg"></div>
<div class="card p-3 mb-3" style="height:206px;border-radius:20px;margin-top:0;"><img class="loadingIcon" src="img/icons/loading/Ellipsis-1.3s-200px.svg"></div>
</div>
<!-- Contains map -->
<div id="map"></div>
</main>
<!-- JS FOR KEYS / API (DARK SKY + MAPBOX + MAPBOX GEOCODER) / FRAMEWORK (BOOTSTRAP) -->
<script src="js/keys.js"></script>
<script src="https://api.mapbox.com/mapbox-gl-js/v1.8.0/mapbox-gl.js"></script>
<script src="js/mapbox-geocoder-utils.js"></script>
<script src="https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-geocoder/v4.4.2/mapbox-gl-geocoder.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/es6-promise@4/dist/es6-promise.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/es6-promise@4/dist/es6-promise.auto.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
<!-- ******** CUSTOM JS ******** -->
<script>
(function() {
"use strict";
// Weather conditions defined by Dark Sky and their corresponding icons
var conditions = [
{name: "clear-day", image: "img/icons/weather/weather_icons-01.svg"},
{name: "clear-night", image: "img/icons/weather/weather_icons-05.svg"},
{name: "rain", image: "img/icons/weather/weather_icons-19.svg"},
{name: "snow", image: "img/icons/weather/weather_icons-31.svg"},
{name: "sleet", image: "img/icons/weather/weather_icons-25.svg"},
{name: "wind", image: "img/icons/weather/weather_icons-57.svg"},
{name: "fog", image: "img/icons/weather/weather_icons-39.svg"},
{name: "cloudy", image: "img/icons/weather/weather_icons-16.svg"},
{name: "partly-cloudy-day", image: "img/icons/weather/weather_icons-17.svg"},
{name: "partly-cloudy-night", image: "img/icons/weather/weather_icons-18.svg"}
];
// Define height of map based on window size
var totalHeight = $(window).height(); // Height of the window
var headerHeight = $('#current').height(); // Height of the header area
var mapHeight = (totalHeight - headerHeight - 20) + "px"; // Map height - accounts for 20px header border also
$('#map').css('height', mapHeight);
// Define function to run on load that grabs current weather and forecast info from Dark Sky API
function getForecast(lat, long) {
$.get("https://cors-anywhere.herokuapp.com/https://api.darksky.net/forecast/" + darkSkyKey + "/" + lat + "," + long + "?").done(function (data) {
// Clear forecast div and change display to flex on load
$('#threeDayForecast').html("");
$('#threeDayForecast').css({
"display": "flex",
"flex-direction": "column"});
// Get date from Dark Sky API call
var currentDateArray = new Date(data.currently.time * 1000).toString().split(" ").slice(0, 4);
var currentDate = "" + currentDateArray[0] + " " + currentDateArray[1] + " " + currentDateArray[2] + ", " + currentDateArray[3]; // Date in 'DayOfWeek Day Month, Year' format
$('#currentDate').html('<h2>' + currentDate + '</h2>');
// Display current temperature in header
$('#currentTemp').text(Math.round(data.currently.temperature) + "°");
// Append to the forecast div the information for specified number of days (in this case, 3 day forecast)
for (var i = 0; i < 3; i++) {
// Create variables for weather data
var day = data.daily.data[i];
var highTemp = Math.round(day.temperatureHigh);
var tempLow = Math.round(day.temperatureLow);
var summary = day.summary;
var humidity = day.humidity;
var wind = day.windSpeed;
var pressure = day.pressure;
var iconIndex = conditions.findIndex(function (item) {
return item.name === day.icon;
});
var icon = conditions[iconIndex].image;
var fullDate = new Date(day.time * 1000).toString();
var dayOfWeek = fullDate.split(" ")[0];
// Create card for each day with weather info and append to forecast div
$('#threeDayForecast').append(
"<div class='card p-3 mb-3' id='day" + i + "Weather'>" +
"<h2 class='dayHeader' id='day" + i + "Date'>" + dayOfWeek + "</h2>" +
// Display only the weather icon and high/low temp on load
"<div class='showOnLoad'>" +
"<img class='weatherIcon' src='" + icon + "'>" +
"<h2 id='day" + i + "HighAndLow'>" +
"<span id='day" + i + "High'>" + highTemp + "</span>° / " +
"<span id='day" + i + "Low'>" + tempLow + "</span>°" +
"</h2>" +
"</div>" +
// Hide summary, humidity, wind, and pressure info until card is clicked
"<p class='hidden' id='day" + i + "Body'>" +
"<span class='summary' id='day" + i + "Summary'>" + summary + "</span><br>" +
"<span id='day" + i + "Humidity'><b>Humidity:</b> " + humidity + "</span>" +
"<br><span id='day" + i + "Wind'><b>Wind:</b> " + wind + "</span>" +
"<br><span id='day" + i + "Pressure'><b>Pressure:</b> " + pressure + "</span>" +
"</p>" +
"</div>");
}
// When card is clicked, expand div and display hidden info:
$('.card').on('click', function() {
$(this).toggleClass('clicked');
$(this).css({
"display": "flex",
"flex-direction": "row",
"justify-content": "space-evenly"
});
$('.dayHeader', this).toggleClass('clicked-header');
$('.hidden', this).slideToggle();
});
});
}
// Display default weather forecast on load for San Antonio, TX
var userLat = 29.4241;
var userLong = -98.4936;
getForecast(userLat, userLong);
// Display map centered at default location
mapboxgl.accessToken = mapboxToken;
var map = new mapboxgl.Map(
{container: 'map',
style: 'mapbox://styles/mapbox/streets-v11',
zoom: 10.5,
center: [userLong, userLat]
});
// Add draggable marker to center of map
var marker = new mapboxgl.Marker({draggable: true, color: 'blueviolet'})
.setLngLat([userLong, userLat])
.addTo(map);
// Create and add geocoder search feature to map
var geocoder = new MapboxGeocoder({
accessToken: mapboxgl.accessToken,
mapboxgl: mapboxgl,
marker: false,
});
map.addControl(
geocoder
);
// When search is performed in geocoder, reset lat and long to user-specified location + update forecast and header info
geocoder.on('result', function(e) {
// Change HTML to loading info for forecast div
$('#threeDayForecast').html(
'<div class="card p-3 mb-3" style="height:206px;border-radius:20px;margin-top:0;"><img class="loadingIcon" src="img/icons/loading/Ellipsis-1.3s-200px.svg"></div>\n' +
'<div class="card p-3 mb-3" style="height:206px;border-radius:20px;margin-top:0;"><img class="loadingIcon" src="img/icons/loading/Ellipsis-1.3s-200px.svg"></div>\n' +
'<div class="card p-3 mb-3" style="height:206px;border-radius:20px;margin-top:0;"><img class="loadingIcon" src="img/icons/loading/Ellipsis-1.3s-200px.svg"></div>');
var lngLat = e.result.geometry.coordinates;
var lat = lngLat[1];
var lng = lngLat[0];
marker.setLngLat(lngLat);
map.zoom = 10.5;
getForecast(lat, lng);
reverseGeocodeCity({lat: lat, lng: lng}, mapboxToken).then(function (results) {
$('#currentLocation').html(results);
});
});
// When marker is dragged, update forecast and geocoding info on drag end
function onDragEnd() {
// Change HTML to loading info for forecast div
$('#threeDayForecast').html(
'<div class="card p-3 mb-3" style="height:206px;border-radius:20px;margin-top:0;"><img class="loadingIcon" src="img/icons/loading/Ellipsis-1.3s-200px.svg"></div>\n' +
'<div class="card p-3 mb-3" style="height:206px;border-radius:20px;margin-top:0;"><img class="loadingIcon" src="img/icons/loading/Ellipsis-1.3s-200px.svg"></div>\n' +
'<div class="card p-3 mb-3" style="height:206px;border-radius:20px;margin-top:0;"><img class="loadingIcon" src="img/icons/loading/Ellipsis-1.3s-200px.svg"></div>');
// Update the lat and long from marker location and use to get new forecast info
var lngLat = marker.getLngLat();
getForecast(lngLat.lat, lngLat.lng);
// Update name of location forecast info being displayed in header
reverseGeocodeCity(lngLat, mapboxToken).then(function (results) {
$('#currentLocation').html(results);
});}
marker.on('dragend', onDragEnd);
})();
</script>
</body>
</html>