-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_weather_api.js
55 lines (52 loc) · 2.66 KB
/
get_weather_api.js
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
// get API
fetch('https://api.openweathermap.org/data/2.5/weather?q=Victoria,CA&APPID=9134f98b590090eb052a46031e0e3ff6')
.then(response => {
console.log(response); // check if js fetch success
return response.json();
})
// decomposite data which send back from api.openweathermap
.then(data => {
console.log(data);
const weatherDescription = data.weather[0].description;
var temperature = data.main.temp;
const humidity = data.main.humidity;
const windSpeed = data.wind.speed;
const weatherImage = document.querySelector('#weather-image');
// pass 4 dynamic data to html file
document.getElementById('weather-description').innerText = weatherDescription;
document.getElementById('temperature').innerText = (temperature - 273.15).toFixed(2); //kelvin to celsius
document.getElementById('humidity').innerText = humidity;
document.getElementById('wind-speed').innerText = windSpeed;
switch (weatherDescription) { // swap dif icons for dif weather condictions
case "broken clouds":
case "overcast clouds":
weatherImage.setAttribute('src', 'https://openweathermap.org/themes/openweathermap/assets/vendor/owm/img/widgets/04d.png');
break;
case "clear sky":
weatherImage.setAttribute('src', 'https://openweathermap.org/themes/openweathermap/assets/vendor/owm/img/widgets/01d.png');
break;
case "few clouds":
weatherImage.setAttribute('src', 'https://openweathermap.org/themes/openweathermap/assets/vendor/owm/img/widgets/02d.png');
break;
case "scattered clouds":
weatherImage.setAttribute('src', 'https://openweathermap.org/themes/openweathermap/assets/vendor/owm/img/widgets/03d.png');
break;
case "shower rain":
weatherImage.setAttribute('src', 'https://openweathermap.org/themes/openweathermap/assets/vendor/owm/img/widgets/09d.png');
break;
case "rain":
weatherImage.setAttribute('src', 'https://openweathermap.org/themes/openweathermap/assets/vendor/owm/img/widgets/10d.png');
break;
case "thunderstorm":
weatherImage.setAttribute('src', 'https://openweathermap.org/themes/openweathermap/assets/vendor/owm/img/widgets/11d.png');
break;
case "snow":
weatherImage.setAttribute('src', 'https://openweathermap.org/themes/openweathermap/assets/vendor/owm/img/widgets/13d.png');
break;
case "mist":
weatherImage.setAttribute('src', 'https://openweathermap.org/themes/openweathermap/assets/vendor/owm/img/widgets/50d.png');
break;
default:
console.log("Unknown condiction!");
}
});