-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather_map.html
251 lines (208 loc) · 8.37 KB
/
weather_map.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>First Mapbox Map</title>
<!-- Mapbox CSS -->
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.53.0/mapbox-gl.css' rel='stylesheet'/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="js/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js"
integrity="sha384-wHAiFfRlMFy6i5SRaxvfOCifBUQy1xHdJ/yoi7FRNXMRBu5WHdZYu1hA6ZOblgut"
crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js"
integrity="sha384-B0UglyR+jN6CkvvICOB2joaf5I4l3gm9GU6Hc1og6Ls7i6U/mkkaduKaBhlAXv9k"
crossorigin="anonymous"></script>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no'/>
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v1.3.1/mapbox-gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v1.3.1/mapbox-gl.css' rel='stylesheet'/>
<!-- Bootstrap -->
<meta http-equiv="x-ua-compatible" content="ie=edge"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css"
integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
<!--
how to get the css filter for svgs
https://codepen.io/sosuke/pen/Pjoqqp
-->
<!-- Your custom styles go here -->
<style>
#map {
/* the width and height may be set to any size */
width: 100%;
height: 400px;
}
</style>
<script src="js/keys.js"></script>
<script src="js/mapbox-utils.js"></script>
</head>
<body>
<h1 id="currentLocation">San Antonio</h1>
<div class="container">
<div class="row">
<div id="forecast" class="row">
</div>
</div>
<div class="row">
<div class="col-md-12">
<!-- The HTML element that serves as the Mapbox container -->
<div id='map'></div>
<pre id='coordinates' class='coordinates'></pre>
</div>
</div>
</div>
<style>
.coordinates {
background: rgba(0, 0, 0, 0.5);
color: #fff;
position: absolute;
bottom: 40px;
left: 10px;
padding: 5px 10px;
margin: 0;
font-size: 11px;
line-height: 18px;
border-radius: 3px;
display: none;
}
</style>
<!-- Bootstrap -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js"
integrity="sha384-wHAiFfRlMFy6i5SRaxvfOCifBUQy1xHdJ/yoi7FRNXMRBu5WHdZYu1hA6ZOblgut"
crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js"
integrity="sha384-B0UglyR+jN6CkvvICOB2joaf5I4l3gm9GU6Hc1og6Ls7i6U/mkkaduKaBhlAXv9k"
crossorigin="anonymous"></script>
<!-- Your custom JS goes here -->
<!-- Mapbox JS -->
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.53.0/mapbox-gl.js'></script>
<script src='https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-geocoder/v4.4.1/mapbox-gl-geocoder.min.js'></script>
<link rel='stylesheet'
href='https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-geocoder/v4.4.1/mapbox-gl-geocoder.css'
type='text/css'/>
<!-- Custom JS -->
<script>
"use strict";
var $j = jQuery.noConflict();
$(document).ready(function () {
let weatherIndex = [
"clear-day", "clear-night", "rain", "snow", "sleet", "wind", "fog", "cloudy", "partly-cloudy-day", "partly-cloudy-night", "hail", "thunderstorm", "tornado"];
let weatherIcon = [
"icon/SVG/Sun.svg",
"icon/SVG/Moon.svg",
"icon/SVG/Cloud-Rain.svg",
"icon/SVG/Cloud-Snow.svg",
"icon/SVG/Cloud-Rain.svg",
"icon/SVG/Cloud-Wind.svg",
"icon/SVG/Cloud-Fog.svg",
"icon/SVG/Cloud-Fog.svg",
"icon/SVG/Cloud-Sun.svg",
"icon/SVG/Cloud-Moon.svg",
"icon/SVG/Cloud-Hail.svg",
"icon/SVG/Cloud-Tornado.svg",
"icon/SVG/Cloud-Tornado.svg"
];
// Map set to San Antonio
/**
* Initial display set to San Antonio
* **/
mapboxgl.accessToken = mapboxToken;
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/streets-v9',
zoom: 10,
center: [-98.4916, 29.4252]
});
var marker = new mapboxgl.Marker({
draggable: true
})
.setLngLat([-98.4916, 29.4252])
.addTo(map);
getWeatherInfo([-98.4916, 29.4252]);
function displayPage(location) {
var currentCenter = {
lng: location[0],
lat: location[1]
}
reverseGeocode(currentCenter, mapboxToken).then(function (results) {
//
console.log(results);
$('#currentLocation').html(results);
getWeatherInfo(location);
drawMap(location);
});
}
function drawMap(location) {
marker = new mapboxgl.Marker({
draggable: true
})
.setLngLat(location)
.addTo(map);
marker.on('dragend', onDragEnd);
return map;
}
/**
*
* Marker functionality
*
* */
function onDragEnd(e) {
// Drag the marker
var lngLat = marker.getLngLat();
map.flyTo({
center: [
lngLat.lng,
lngLat.lat]
});
displayPage([lngLat.lng, lngLat.lat]);
}
marker.on('dragend', onDragEnd);
/**
*
* Search textbox functionality
* */
var searchNewLocation = new MapboxGeocoder({
accessToken: mapboxgl.accessToken,
mapboxgl: mapboxgl
});
map.addControl(searchNewLocation);
searchNewLocation.on('result', function (e) {
//call new location
displayPage(e.result["center"]);
})
// Add zoom and rotation controls to the map.
map.addControl(new mapboxgl.NavigationControl());
/**
*
* Get the weather information
*
* */
function getWeatherInfo(location) {
let weatherKey = "https://cors-anywhere.herokuapp.com/https://api.darksky.net/forecast/" + darkKey + "/" + location[1] + "," + location[0];
// console.log(weatherKey);
$.get(weatherKey).done(function (weatherData) {
let newData = " <table id=\"forecast\" style='width:100%'><tr style=\"align-content: center\">";
let weatherForecast = weatherData["daily"].data;
for (let i = 0; i < 3; i++) {
var forecast = (Math.round(weatherForecast[i]["apparentTemperatureHigh"]) + '°' + '/' + Math.round(weatherForecast[i]["apparentTemperatureLow"]) + '°').trim();
newData += '<td>'
newData += '<div class="center"><h3>' + forecast + '</h3></div>'
newData += '<div class="center"> <img src="' + weatherIcon[weatherIndex.indexOf(weatherForecast[i]["icon"])] + '"> </div>'
newData += ' <div class="center">' + weatherForecast[i]["summary"] + '</div>'
newData += ' <div class="center"><strong>Humidity: </strong>' + weatherForecast[i]["humidity"] + '</div>';
newData += ' <div class="center"><strong>Wind:</strong>' + weatherForecast[i]["windSpeed"] + '</div>';
newData += ' <div class="center"><strong>Pressure</strong>' + weatherForecast[i]["pressure"] + '</div>';
newData += ' </div></td>'
}
// console.log(newData);
newData += "</tr></table>";
$("#forecast").replaceWith(newData);
});
}
})
</script>
</body>
</html>