Current Weather App for FreeCodeCamp

In this example below you will see how to do a Current Weather App for FreeCodeCamp with some HTML / CSS and Javascript

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

Technologies

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

<head>
  <meta charset="UTF-8">
  <title>Current Weather App for FreeCodeCamp</title>
  
  
  <link rel='stylesheet prefetch' href='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css'>

  
  
</head>

<body>

  <html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">

<link rel="stylesheet" href="https://opensource.keycdn.com/fontawesome/4.7.0/font-awesome.min.css" integrity="sha384-dNpIIXE8U05kAbPhy3G1cz+yZmTzA6CY8Vg/u2L9xRnHjJiAK76m2BIEaSEV+/aU" crossorigin="anonymous">

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-social/5.1.1/bootstrap-social.css" />

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

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

<script
  src="https://code.jquery.com/jquery-3.1.1.min.js"
  integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="
  crossorigin="anonymous"></script>

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

</head>
<body>
  <script>
$(document).ready(function() {
  var myAPIKey = "b26af0e12bcd313af89b5cd801f3c0d1";
  $.getJSON("http://ip-api.com/json", function(data) {
  $("#city").text(data.city + ", " + data.country);
  determineWeather(data.lat, data.lon);
})
function determineWeather(lat, lon) {
  var weatherURL = 'http://api.openweathermap.org/data/2.5/weather?lat=' + lat + "&lon=" + lon + "&units=imperial" + "&APPID=" + myAPIKey;
  $.getJSON(weatherURL, function(weatherInfo){

    $("#temp").text(weatherInfo.main.temp);
    var quelTemps = weatherInfo.weather[0].icon;
      $("#state").text(weatherInfo.weather[0].icon);
    var weatherState = quelTemps.substring(0,2);
    switch(weatherState) {
      case "01":
       $("#state").html("<i class='wi wi-day-sunny'></i>");
       break;
   case "02":
       //few clouds
       $("#state").html("<i class='wi wi-cloud'></i>");
       break;
   case "03":
   $("#state").html("<i class='wi wi-cloudy'></i>");
      //scattered clouds
       break;
   case "04":
   $("#state").html("<i class='wi wi-day-sunny'></i>");
       //broken clouds
       break;
   case "09":
   $("#state").html("<i class='wi wi-day-showers'></i>");
      //shower rain
       break;
   case "10":
    //rain
    $("#state").html("<i class='wi wi-day-rain'></i>");
       break;
   case "11":
   $("#state").html("<i class='wi wi-day-lightning'></i>");
       //thunderstorm
       break;
    case "13":
    $("#state").html("<i class='wi wi-day-snow'></i>");
    //snow
    break;
  case "50":
  $("#state").html("<i class='wi wi-day-haze'></i>");
  //mist
  break;
    }
  })
}
});
  </script>
<body>
  <div class="container-fluid">
	<div class="row">
		<div class="col-md-12">
			<div class="row">
				<div class="col-md-3">
				</div>
				<div class="col-md-6 text-center">
					<h3 id="test">
						Current Weather App
					</h3>
          <br />
          <div id="state" style="font-size:100px">
                   <p>

                   </p>
                 </div><br />
          <div>
            <p>
              You are in:
            </p><p id="city">

            </p>
          </div>
          <br /><div>
            <p>
              The current temperature is:
            </p>
            <p id="temp">

            </p>
        <form name="new_form">
        <div class="radio_group_wrap"><label class="radio-inline" id="degreeF"><input checked type="radio" name="radioField" value="option0">&#176; Fahrenheit </label><label class="radio-inline" id="degreeC"><input type="radio" name="radioField" value="option1">&#176; Celsius </label></div></form>
          </div>
				</div>
				<div class="col-md-3">
				</div>
			</div>
		</div>
	</div>
</div>
</body>
</html
  <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.0/jquery-ui.min.js'></script>

  

</body>

</html>

Comments