Advertisement
Luninariel

Weather Server - WIP

Mar 4th, 2020
535
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.     <title>CSC 346 Weather Midterm</title>
  7.     <style>
  8.     #city, #weather {visibility: hidden;}
  9.     </style>
  10.     <script>
  11.  
  12.         //Variable for API Key
  13.          const apiKey = "&appid=9e4a3d156fc79cf7f5d851d7c30520d1";
  14.  
  15.          //Variable for API URL
  16.          const URL = "https://api.openweathermap.org/data/2.5/weather?";
  17.  
  18.         //Variable to hold the units we want weather in (Farenheit)
  19.          const measure = "&units=imperial";
  20.  
  21.         //Variable to hold the Cors that Noynaert says I need because reasons
  22.          const cors = "https://cors-anywhere.herokuapp.com/";
  23.  
  24.  
  25.          //Function to display the temperature in the temp space
  26.          function showTemp(temp){
  27.              let foundTemp = document.getElementById("tempSpace");
  28.              foundTemp.innerHTML = temp;
  29.          }
  30.  
  31.         //Function to display the description in the description space
  32.         function showDesc(desc){
  33.              let foundDesc = document.getElementById("descSpace");
  34.              foundDesc.innerHTML = desc;
  35.          }
  36.  
  37.          //Function to display the Wind Speed in the windspeed space
  38.          function showSpeed(speed){
  39.              let foundSpeed = document.getElementById("speedSpace");
  40.              foundSpeed.innerHTML = speed;
  41.          }
  42.  
  43.          //Function to display the Wind Direction in the direction space
  44.          function showDirection(direction){
  45.              let foundDirection = document.getElementById("directionSpace");
  46.              foundDirection.innerHTML = direction;
  47.          }
  48.  
  49.          //Function to display the Location name in the name space
  50.          function showName(name){
  51.              let foundName = document.getElementById("nameSpace");
  52.              foundName.innerHTML = name;
  53.          }
  54.  
  55.         //Function to fetch and process the weather for the selected area.
  56.         function getWeather(weatherAPI){
  57.             console.log("You're in getWeather()");
  58.             //Fetch the weather
  59.             fetch(weatherAPI)
  60.             .then(response => {
  61.                 console.log(response);
  62.                 return response.json();
  63.             })
  64.             //Process the payload and extract the bits we need
  65.             .then(payload => {
  66.                 console.log("This is your Payload: ");
  67.                 console.log(payload);
  68.  
  69.                 console.log("This is your Temp: ");
  70.                 console.log(payload.main.temp);
  71.                 showTemp(payload.main.temp);
  72.  
  73.                 console.log("This is your Description: ");
  74.                 console.log(payload.weather[0].description);
  75.                 showDesc(payload.weather[0].description);
  76.  
  77.                 console.log("This is your Wind Speed: ");
  78.                 console.log(payload.wind.speed);
  79.                 showSpeed(payload.wind.speed);
  80.  
  81.                 console.log("This is your Wind Direction: ");
  82.                 console.log(payload.wind.deg);
  83.                 showDirection(payload.wind.deg);
  84.  
  85.                 console.log("This is your Location Name: ")
  86.                 console.log(payload.name);
  87.                 showName(payload.name);
  88.             })
  89.  
  90.         }
  91.  
  92.         //Function to pass the value selected by the user in the City question
  93.         function showWeather(me){
  94.             var coordinates = me.value.split(";")
  95.             console.log("The lat of the city you selected is: "+ coordinates[0]);
  96.             console.log("The long of the city you selected is: "+ coordinates[1]);
  97.            
  98.             weather.style.visibility="visible";
  99.             let lat = "lat=" + coordinates[0];
  100.             let long = "&lon=" + coordinates[1].trim();
  101.             let weatherAPI = cors+URL+lat+long+apiKey+measure;
  102.             console.log(weatherAPI);
  103.             getWeather(weatherAPI);
  104.            
  105.         }
  106.  
  107.         //Function to pass the value selected by the user to getCity for use
  108.         function showQ2(me){
  109.             console.log("You've selected the state: "+me.value);
  110.             city.style.visibility="visible";
  111.             getCity('locations/cities/'+me.value+'.json');
  112.            
  113.         }
  114.         //Listener to run on page load, and get the file that lists all states
  115.         window.addEventListener('load', (event) => {
  116.         getStates('locations/cities/00states.json');
  117.             });
  118.        
  119.         //Function to display the states as option values within the state select drop down
  120.         function displayStates(states){
  121.             let y = ' ';
  122.             let x = ' ';
  123.  
  124.             x+= `<option value="" disabled selected>Choose State</option>`
  125.             for(i in states){
  126.                
  127.                 y+= `<option value="${states[i].code}">${states[i].state}</option>`
  128.  
  129.             }
  130.             document.getElementById('states').innerHTML = x+y;
  131.         }
  132.         //Function to display the Cities as option values within the city select drop down
  133.         function displayCity(cities){
  134.             let y = ' ';
  135.             let x = ' ';
  136.  
  137.             x+= `<option value="" disabled selected>Choose City</option>`
  138.             for(i in cities){
  139.                
  140.                 y+= `<option value="${cities[i].lat}; ${cities[i].long}">${cities[i].city}</option>`
  141.  
  142.             }
  143.             document.getElementById('cities').innerHTML = x+y;
  144.  
  145.         }
  146.         //Function to Fetch and Read the File of states
  147.         function getStates(fileName){
  148.             //Fetch the file
  149.             fetch(fileName)
  150.             .then(response => {
  151.                 return response.json();
  152.             })
  153.             //Read the File of states and display it
  154.             .then(list => {
  155.                 displayStates(list);
  156.             })
  157.             .catch( err => console.log("Caught Error is" + err));
  158.         }
  159.  
  160.           //Function to fetch the relevant city file after a state is chosen in showQ2
  161.          function getCity(stateName){
  162.             //Fetch the file
  163.             fetch(stateName)
  164.             .then(response => {
  165.                 return response.json();
  166.             })
  167.             //Read the file and display its contents
  168.             .then(list => {
  169.                 displayCity(list);
  170.             })
  171.             .catch( err => console.log("Caught Error is" + err));
  172.         }
  173.     </script>
  174. </head>
  175. <body>
  176.     <h1>Local Weather</h1>
  177.     <div id="state">
  178.         <h2>Please choose a State: </h2>
  179.        
  180.         <select id="states" name="state" form="stateform" onchange="showQ2(this)">
  181.        
  182. </select>
  183.  
  184.     </div>
  185.     <div id ="city">
  186.         <h2>Please choose a City: </h2>
  187.         <select id = "cities" name="city" form="cityform" onchange="showWeather(this)">
  188.  
  189.         </select>
  190.  
  191.  
  192.  
  193.     </div>
  194.     <div id = "weather">
  195.         <h2>The Weather For Your Selected City Is:</h2>
  196.         <p>Temperature: <span id = "tempSpace">&nbsp;</span></p>
  197.         <p>Description: <span id = "descSpace">&nbsp;</span></p>
  198.         <p>Wind Speed: <span id = "speedSpace">&nbsp;</span></p>
  199.         <p>Wind Direction: <span id = "directionSpace">&nbsp;</span></p>
  200.         <p>Location Name: <span id = "nameSpace">&nbsp;</span></p>
  201.     </div>
  202. </body>
  203. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement