forked from toy101/watch_and_weather_for_obs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetWeather.js
144 lines (134 loc) · 5.03 KB
/
GetWeather.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
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
/*
真下にあるapiKeyの左辺を
取得したAPIキーに置き換えてください(ダブルクォーテーションの間に)
API取得方法の記事(https://auto-worker.com/blog/?p=1612#toc_id_4)
*/
var apiKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
// 天気予報データの処理
// 都市のデータ
/*
データ構造
cityName: htmlで使用するid
displayName: 表示する文字列
lat: 緯度(Google Mapの左の数値)
lon: 経度(Google Mapの右の数値)
*/
let localDatas = [
{
cityName: "Sapporo",
displayName: "札幌",
lat: 43.07579931130803,
lon: 141.35486686261726,
},
{
cityName: "Sendai",
displayName: "仙台",
lat: 38.28154933433309,
lon: 140.86705410133413,
},
{
cityName: "Tokyo",
displayName: "東京",
lat: 35.688126732985125,
lon: 139.76939345848703,
},
{
cityName: "Kanazawa",
displayName: "金沢",
lat: 36.580664669560086,
lon: 136.65969926711716,
},
{
cityName: "Nagoya",
displayName: "名古屋",
lat: 35.257094766996595,
lon: 136.91392774706733,
},
{
cityName: "Osaka",
displayName: "大阪",
lat: 34.724827141227856,
lon: 135.51354211321944,
},
{
cityName: "Okayama",
displayName: "岡山",
lat: 34.68943160430332,
lon: 133.92754628644,
},
{
cityName: "Fukuoka",
displayName: "福岡",
lat: 33.56735024709979,
lon: 130.36474036426256,
},
];
// スライドアニメーションのイージングの計算
let slideAnime = `@keyframes slideAnime
{ 0% { top : -100%;}
${(100 / localDatas.length) * 0.1}% { top: 0;}
${(100 / localDatas.length) * 0.9}% { top: 0;}
${100 / localDatas.length}% { top: 100%;}
100% { top: 100%;}`;
let css = document
.getElementById("css")
.insertAdjacentHTML("afterbegin", slideAnime);
let weatherList = document.getElementById("weather-list");
localDatas.forEach((city, idx) => {
// 天気APIリクエスト
const query_params = new URLSearchParams({
appid: apiKey,
lat: city.lat,
lon: city.lon,
lang: "ja",
units: "metric",
exclude: "current,minutely,alerts",
// , "minutely", "daily", "alerts"
});
fetch("https://api.openweathermap.org/data/2.5/onecall?" + query_params)
.then((response) => {
return response.json();
})
.then((data) => {
responsedWeather = data.daily[0];
console.log(responsedWeather);
weatherList.innerHTML += `<div class="weather" id=${city.cityName}>
<div class="text-data">
<div class="local-label">${
city.displayName
}</div>
<div class="weather-values">
<div class="min-temp">${
Math.round(
responsedWeather
.temp.min
) + "℃"
}</div>
<div class="max-temp">${
Math.round(
responsedWeather
.temp.max
) + "℃"
}</div>
</div>
</div>
<img
class="weather-icon"
id=${"img-" + city.cityName}
src=${
"https://openweathermap.org/img/w/" +
responsedWeather.weather[0]
.icon +
".png"
}
alt="Weather icon"
/>
</div>`;
// 数に応じたCSSの調整
weatherBlock = document.getElementById(city.cityName);
weatherBlock.style.animation = `slideAnime ${
localDatas.length * 4
}s ease infinite`;
weatherBlock.style.animationDelay = 4 * idx + "s";
});
});