Weather // FreeCodeCamp Intermediate Frontend Project

In this example below you will see how to do a Weather // FreeCodeCamp Intermediate Frontend Project with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by fmorisan, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright fmorisan ©

Technologies

  • HTML
  • CSS
  • JavaScript
<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>Weather // FreeCodeCamp Intermediate Frontend Project</title>
  
  
  <link rel='stylesheet prefetch' href='https://cdnjs.cloudflare.com/ajax/libs/ionicons/2.0.1/css/ionicons.min.css'>

      <link rel="stylesheet" href="css/style.css">

  
</head>

<body>

  <div class="center">
  <h1>
    Weather for <span id="location"></span>
  </h1>
  <small>by Mori</small>
  <h2><span id="temp"></span><span id="unit"></span></h2>
  <div id="weatherIcon" class="icon"></div>
  <h2 id="skyState"></h3>
  <small id="extendedState"></small>
  <div class="row">
    <div>
      <h3>Wind</h3>
      <span id="windVel"></span> knots <br />
      coming from <span id="windDir"></span> degrees
    </div>
    <div>
      <h3>Cycles</h3>
      Sunrise: <span id="sunriseTime"></span><br />
      Sunset: <span id="sunsetTime"></span>
    </div>
  </div>
</div>
  <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js'></script>

  

    <script  src="js/index.js"></script>




</body>

</html>

/*Downloaded from https://www.codeseek.co/fmorisan/weather-freecodecamp-intermediate-frontend-project-WGRQbZ */
@import 'https://fonts.googleapis.com/css?family=Exo+2';

body {
  font-family: 'Exo 2', sans-serif;
  background-color: #222;
  color: #999;
  text-align: center;
}

.row div {
  display: inline-block;
  margin: 0px 30px 0px 30px;
}

#weatherIcon {
  font-size: 5em;
  color: #ccc;
}

#unit {
  text-shadow: 0px 0px 5px #444;
  color: #aab;
}

/*Downloaded from https://www.codeseek.co/fmorisan/weather-freecodecamp-intermediate-frontend-project-WGRQbZ */
function getAPIData(celsius){
  if ("geolocation" in navigator){
    navigator.geolocation.getCurrentPosition(
      function(position){
        $.ajax({
          url: "http://api.openweathermap.org/data/2.5/weather?lat="+position.coords.latitude+"&lon="+position.coords.longitude+"&appid=29ab8d5aaa0e4a1d5773b49cd981f648",
          success: function(data){
            console.log(position);
            console.log(data);
            if (data.hasOwnProperty("cod") && (data.cod == 404)){
              alert(data.message)
            } else {
              console.log(data);
              $("#location").html(data.name+", "+ data.sys.country);
              $("#skyState").html(data.weather[0].main);
              $("#extendedState").html(data.weather[0].description);
              $("#windVel").html(data.wind.speed);
              $("#windDir").html(data.wind.deg);
              var sun = new Date(0);
              sun.setUTCSeconds(data.sys.sunrise)
              $("#sunriseTime").html(sun.toLocaleTimeString());
              var set = new Date(0);
              set.setUTCSeconds(data.sys.sunset);
              $("#sunsetTime").html(set.toLocaleTimeString());

              var iconClasses = {
                "01": "ion-ios-sunny",
                "02": "ion-ios-partlysunny",
                "03": "ion-ios-cloudy",
                "04": "ion-ios-cloudy",
                "09": "ion-ios-rainy",
                "10": "ion-ios-rainy",
                "11": "ion-ios-thunderstorm",
                "13": "ion-ios-snowy"
              } 
              var iconCode = data.weather[0].icon.slice(0,2);
              $("#weatherIcon").addClass(iconClasses[iconCode]);
              var temp = (data.main.temp - 273.15).toFixed(1);
              if (!celsius){
                $("#temp").html((temp*9/4+32).toFixed(0));
                $("#unit").html(" F");
              } else {
                $("#temp").html(temp);
                $("#unit").html(" C");
              }
            }
          }
        });
      });
  }
}
var celsius = true;
$("#unit").on(
  "click",
  function(){
    celsius = !celsius;
    getAPIData(celsius);
  }
);
getAPIData(true);

Comments