top of page
検索

(第1回)[Javascript]OpenWeatherMapからの天気情報の取得とその表示

  • 執筆者の写真: ℃
  • 2018年1月4日
  • 読了時間: 4分

更新日:2018年1月10日

 OpenWeatherMapというサイトを使って指定地域の天気情報を取得して、表示させてみたいと思います。まず最初にやることは、OpenWeatherMapサイトにとんで無料登録して自分のAPI keysを取得してください。


[html]

  1. <!DOCTYPE html>

  2. <html>

  3. <head>

  4. <meta charset="utf-8">

  5. <title>お天気情報取得</title>

  6. <link rel="stylesheet" type="text/css" media="screen,print" href="style.css" />

  7. <!--フォント-->

  8. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

  9. <!--アイコン-->

  10. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/weather-icons/2.0.9/css/weather-icons.css">

  11. </head>

  12. <body>

  13. <div id="wrap"></div>

  14. <div id="weatherbox"><!--外枠-->

  15. <div id="topbox"><!--都市名ボックス-->

  16. <span id="city"></span>

  17. </div>

  18. <div id="middlebox"><!--天候詳細ボックス1-->

  19. <div id="midmid">

  20. <span><i id="currenticon"></i></span>

  21. <span id="temp"></span>

  22. <button class="fc">C</button>

  23. </div>

  24. <span id="desc"></span>

  25. </div><!--/middlebox-->

  26. <div id="bottombox"><!--天候詳細ボックス2-->

  27. <div class="bottomsides">

  28. <span><i class="wi wi-raindrop"></i></span>

  29. <span id="hum"></span>

  30. </div>

  31. <div class="bottomsides">

  32. <span><i class="wi wi-strong-wind"></i></span>

  33. <span id="wind"></span>

  34. </div>

  35. </div><!--/bottombox-->

  36. </div><!--/weatherbox-->

  37. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

  38. <script src="main.js"></script>

  39. </body>

  40. </html>



[css]

  1. /* Stylesheet for Local Weather App */

  2. @import url('https://fonts.googleapis.com/css?family=Montserrat');


  3. html {

  4. height: 100%;

  5. /*

  6. background: linear-gradient(to bottom, #4C739B 0%, #B0D5A7 100%) fixed;

  7. background: -webkit-linear-gradient(to bottom, #4C739B 0%, #B0D5A7 100%) fixed;

  8. background: -moz-linear-gradient(to bottom, #4C739B 0%, #B0D5A7 100%) fixed;

  9. */

  10. }

  11. body{

  12. margin:0;

  13. }

  14. #wrap{

  15. position: absolute;

  16. width: 100%;

  17. height: 100%;

  18. }

  19. #weatherbox {

  20. /*box-shadow: 2px 2px 10px #FFFFFF;*/

  21. border: 1px solid #FFFFFF;

  22. color: #FFFFFF;

  23. display: flex;

  24. flex-direction: column;

  25. justify-content: center;

  26. position: absolute;

  27. right: 1%;

  28. top: 1%;

  29. margin: auto;

  30. max-width: 100%;

  31. max-height: 100%;

  32. overflow: auto;

  33. width: 200px;

  34. height: 250px;

  35. }

  36. #topbox {

  37. border-bottom: 1px #FFFFFF solid;

  38. height: 20%;

  39. display: flex;

  40. flex-direction: column;

  41. justify-content: center;

  42. align-items: center;

  43. font-family: 'Montserrat', sans-serif;

  44. font-size: 16px;

  45. }

  46. #middlebox {

  47. height: 50%;

  48. display: flex;

  49. flex-direction: column;

  50. justify-content: center;

  51. align-items: center;

  52. font-family: 'Montserrat', sans-serif;

  53. }

  54. #midmid {

  55. display: flex;

  56. flex-direction: row;

  57. align-items: center;

  58. }

  59. #bottombox {

  60. height: 30%;

  61. display: flex;

  62. flex-direction: row;

  63. justify-content: space-around;

  64. align-items: center;

  65. font-family: 'Montserrat', sans-serif;

  66. }

  67. .bottomsides {

  68. height: 100%;

  69. display: flex;

  70. flex-direction: column;

  71. justify-content: center;

  72. align-items: center;

  73. }

  74. .wi {

  75. width: 100%;

  76. font-size: 35px;

  77. }

  78. button {

  79. color: #FFFFFF;

  80. outline: none;

  81. }

  82. #temp {

  83. font-size: 35px;

  84. padding-left: 10px;

  85. }

  86. .fc {

  87. border: none;

  88. border-radius: 50%;

  89. font-size: 35px;

  90. height: 1.5em;

  91. width: 1.5em;

  92. text-align: center;

  93. text-decoration: none;

  94. background: none;

  95. cursor: pointer;

  96. padding: 0px;

  97. }

  98. .fc:hover {

  99. color: #FFFFFF;

  100. border: 1px solid #FFFFFF;

  101. /*color: #4C739B;

  102. background-color: #FFFFFF;*/

  103. }


[js]

  1. // JS for Local Weather App

  2. $( document ).ready(function() {


  3. //使用する天気情報の指定。

  4. var lat;//軽度

  5. var lon;//緯度

  6. var desc;//天気の詳細

  7. var hum;//湿度

  8. var weatherid;

  9. var temp = [];//気温

  10. var wind = [];//風速


  11. /*

  12. ユーザーのIPアドレスを取得して場所や座標を取得する。

  13. getJSON関数を使ってjsonデータを読み込んで使用する。

  14. 第一引数にjsonファイルのurlを指定して、第二引数にfunction(変数名)と書き、jsonデータをそのまま変数の中に入れる。

  15. */


  16. $.getJSON("https://ipapi.co/json", function(data) {


  17. //$("#city").html(data.city);緯度軽度を指定して都市を指定する。

  18. lat = 43.06451;//軽度

  19. lon = 141.346603;//緯度


  20. /*

  21. 天気情報を取得する。

  22. OpenWeatherMapでAPI keysの取得が必要。

  23. http://api.openweathermap.org/data/2.5/weather?lat=軽度値&lon=緯度値&appid=API keysにリクエストして天気のデータを取得する。

  24. */

  25. $.getJSON("http://api.openweathermap.org/data/2.5/weather?lat=" + lat + "&lon=" + lon +"&APPID=****************************", function(weatherData) {


  26. //HTMLの変更。$('A').html('HTMLを記述'); AのHTMLを記述したHTMLに変更。

  27. //<span id="city"></span>に都市名を代入。

  28. $("#city").html(weatherData.name);

  29. /*

  30. Store temperates (celsius and fahrenheit)

  31. toFixed()少数切り捨て。

  32. &#176アイコンid

  33. 気温が絶対温度[K]で返ってくるので、セルシウス温度[℃]と華氏度に[°F]に変換する。

  34. t[℃] = T[K]-273.25 = 5(f[°F]-32)/9

  35. */

  36. //温度の取得。

  37. temp[0] = (weatherData.main.temp - 273.15).toFixed(0) + "&#176";

  38. temp[1] = (((weatherData.main.temp - 273.15)*1.8) + 32).toFixed(0) + "&#176";


  39. //天気の詳細

  40. desc = weatherData.weather[0].description;

  41. //湿度

  42. hum = weatherData.main.humidity + '%';


  43. //風速(mi/h:マイル毎時)

  44. //1[m/s] = 3.6[km/s], 1[m/s] = 2.23[mi/h]

  45. wind[0] = (weatherData.wind.speed * 3.6).toFixed(2) + " km/h";

  46. wind[1] = (weatherData.wind.speed * 2.23).toFixed(2) + " mi/h";


  47. //アイコン

  48. weatherid = (weatherData.weather[0].id);


  49. /*

  50. 各数値はOpenWeatherMapが設定している Weather condition codes

  51. このコードによって既存のアイコンが設定されている。

  52. これを各々 Maintained at http://erikflowers.github.io/weather-icons で提供されているアイコンに変換する。

  53. */

  54. if (weatherid == 800) {

  55. $("#currenticon").html('<i class="wi wi-day-sunny></i>').css({"color": "#FFE14A"});

  56. };


  57. var rainIcons = [300, 301, 302, 310, 311, 312, 313, 314, 321, 500, 501, 502, 503, 504, 511, 520, 521, 522, 531];

  58. /*

  59. for文は変数iを宣言し、これを0に初期化することからはじめる。iがrainIcons.lengthより小さいことを

  60. チェックしてから続く文を実行し、ループを通過するごとにiを1づつ増加させる。

  61. iは配列[]の番号。

  62. rainIcons.lengthはrainIconsが持つ配列の長さ。

  63. */

  64. for (i=0; i<rainIcons.length; i++) {

  65. if (weatherid == rainIcons[i]) {

  66. $("#currenticon").html('<i class="wi wi-rain"></i>').css({"color": "#244A69"});

  67. $("#wrap").html('<div id="wrapper"></div>').css({"width":"100%","height":"100%","position":"absolute","background":"#aaa","z-index":"-1"});

  68. };

  69. };


  70. var stormIcons = [200, 201, 202, 210, 211, 212, 221, 230, 231, 232];

  71. for (i=0; i<stormIcons.length; i++) {

  72. if (weatherid == stormIcons[i]) {

  73. $("#currenticon").html('<i class="wi wi-storm-showers"></i>').css({"color": "#244A69"});

  74. $("#wrap").html('<div id="wrapper"></div>').css({"width":"100%","height":"100%","position":"absolute","background":"#aaa","z-index":"-1"});

  75. };

  76. };


  77. var cloudIcons = [801, 802, 803, 804];

  78. for (i=0; i<cloudIcons.length; i++) {

  79. if (weatherid == cloudIcons[i]) {

  80. $("#currenticon").html('<i class="wi wi-cloudy"></i>').css({"color": "#C1C1C1"});

  81. $("#wrap").html('<div id="wrapper"></div>').css({"width":"100%","height":"100%","position":"absolute","background":"#aaa","z-index":"-1"});

  82. };

  83. };

  84. var snowIcons = [600, 601, 602, 611, 612, 615, 616, 620, 621, 622];

  85. for (i=0; i<snowIcons.length; i++) {

  86. if (weatherid == snowIcons[i]) {

  87. $("#currenticon").html('<i class="wi wi-snow"></i>').css({"color": "#FFFFFF"});

  88. $("#wrap").html('<div id="wrapper"></div>').css({"width":"100%","height":"100%","position":"absolute","background":"#aaa","z-index":"-1"});

  89. };

  90. };


  91. var atmosphereIcons = [701, 711, 721, 731, 741, 751, 761, 762, 771, 781];

  92. for (i=0; i<atmosphereIcons.length; i++) {

  93. if (weatherid == atmosphereIcons[i]) {

  94. $("#currenticon").html('<i class="wi wi-dust"></i>').css({"color": "#C1C1C1"});

  95. $("#wrap").html('<div id="wrapper"></div>').css({"width":"100%","height":"100%","position":"absolute","background":"#aaa","z-index":"-1"});

  96. };

  97. };


  98. var extremeIcons = [900, 901, 902, 903, 904, 905, 906];

  99. for (i=0; i<extremeIcons.length; i++) {

  100. if (weatherid == extremeIcons[i]) {

  101. $("#currenticon").html('<i class="wi wi-storm-warning"></i>').css({"color": "#FFFFFF"});

  102. $("#wrap").html('<div id="wrapper"></div>').css({"width":"100%","height":"100%","position":"absolute","background":"#aaa","z-index":"-1"});

  103. };

  104. };


  105. var additionalIcons = [951, 952, 953, 954, 955, 956, 957, 958, 959, 960, 961, 961];

  106. for (i=0; i<additionalIcons.length; i++) {

  107. if (weatherid == additionalIcons[i]) {

  108. $("#currenticon").html('<i class="wi wi-hurricane-warning"></i>').css({"color": "#FFFFFF"});

  109. $("#wrap").html('<div id="wrapper"></div>').css({"width":"100%","height":"100%","position":"absolute","background":"#aaa","z-index":"-1"});

  110. };

  111. };


  112. //各天気要素の取得情報を各々のidに代入する。

  113. $("#temp").html(temp[0]);

  114. $("#desc").html(desc);

  115. $("#hum").html(hum);

  116. $("#wind").html(wind[0]);

  117. });


  118. //クリック時の入れ替え。

  119. //class="fc"をクリックすると以下の関数を実行する。

  120. $(".fc").click (function() {

  121. //class="fc"のテキスト要素が'F'ならば、lass="fc"のテキスト要素を'C'に変える。

  122. if ($(".fc").text() === "F") {

  123. $(".fc").text("C");

  124. //id="temp","wind"のhtmlの記述をtemp[0],wind[0]に変更。

  125. $("#temp").html(temp[0]);

  126. $("#wind").html(wind[0]);

  127. }

  128. //class="fc"のテキスト要素が'C'ならば、lass="fc"のテキスト要素を'F'に変える。

  129. else {

  130. $(".fc").text('F');

  131. //id="temp","wind"のhtmlの記述をtemp[1],wind[1]に変更。

  132. $("#temp").html(temp[1]);

  133. $("#wind").html(wind[1]);

  134. };

  135. });


  136. //アイコンカラーの設定の確認。

  137. var weatherIcons = {

  138. sunny: ["wi-day-sunny", "#FFE14A"],

  139. cloudy: ["wi-cloudy", "#C1C1C1"],

  140. rainy: ["wi-rain", "#33436D"],

  141. storm: ["wi-storm-showers", "#33436D"],

  142. snow: ["wi-snow", "#FFFFFF"]

  143. };

  144. });

  145. });



 
 
 

Comments


© 2023 by The Book Lover. Proudly created with Wix.com

bottom of page