123

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <div id="wrapper">
  <div id="main">
    <h1>Local Weather Station</h1>
    <h2 id="locale">Loading...</h2>
    <h3 id="error"></h3>
    <div id="icon"></div>
    <span id="temperature"></span>
    <span id="button"></span>
  </div>
  <footer>
    <p>by <a href="https://codepen.io/Feldbot/" target="_blank">Daniel Feldman</a></p>
  </footer>
</div>
            
          
!
            
              @import url('https://fonts.googleapis.com/css?family=Oswald:200,300,400,500,600,700');


#main {
  text-align: center;
  margin-top: 22px;
}
body {
  margin: 0;
  padding: 0;
  width: 100%;
  background-image:
    url("https://raw.githubusercontent.com/Feldbot/Free-Code-Camp-Projects/master/Project-4-Local-Weather/images/pexels-photo-164024-weather.jpeg");
  background-repeat: no-repeat;
  background-attachment: fixed;
  background-position: center;
  background-size: cover;
}
h1, h2, h3, a, p {
  font-family: 'Oswald', sans-serif;
  text-shadow: 1px 1px black;
}
h1 {
  font-size: 4em;
  font-weight: 700;
  color: silver;
}
h2 {
  font-size: 3em;
  font-weight: 400;
  color: goldenRod;
  margin-top: -25px;
}
h3 {
  font-size: 1.7em;
  font-weight: 300;
  color: goldenRod;
}
p, a {
  font-size: 1.2em;
  font-weight: 200;
  color: silver;
  text-decoration: none;
}
a:hover{
  color: lightGoldenRodYellow;
}
#icon {
  background-color: rgba(0,0,0,.22);
  border-radius: 50%;
  display: inline-block;
  width: 50px;
  padding: 10px;
  margin: -20px 0px 0px;
}
#toggleTemp {
  padding: 0px 0px 10px;
  font-family: 'Oswald', sans-serif;
  font-size: 1em;
  background-color: silver;
  width: 100px;
  height: 30px;
  border-radius: 10px;
}
#toggleTemp:hover {
  background-color: lightGoldenRodYellow;
}
footer {
  position: absolute;
  bottom: 0;
  width: 100%;
  height: 100px;
  text-align: center;
}
footer p {
  padding-top: 30px;
}

/* Background image from: https://www.pexels.com/photo/tsunami-cloud-during-golden-hour-164024/ */

            
          
!
            
              var output = $("#locale");
var errorMsg = $("#error");
var latitude, longitude;

function getLocation() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(getPosition, showError);
    console.log(navigator.geolocation);
  } else {
    output.html("Geolocation is not supported by this browser.");
  }
}

function getPosition(position) {
  latitude = position.coords.latitude;
  longitude = position.coords.longitude;

  getWeather(latitude, longitude);
}

function showError(error) {
  output.html('&nbsp;');
  switch (error.code) {
    case error.PERMISSION_DENIED:
      errorMsg.html("User denied the request for Geolocation.");
      break;
    case error.POSITION_UNAVAILABLE:
      output.html("Location information is unavailable.");
      break;
    case error.TIMEOUT:
      output.html("The request to get user location timed out.");
      break;
    case error.UNKNOWN_ERROR:
      output.html("An unknown error occurred.");
      break;
  }
}

function getWeather() {
  $.ajax({
    url: "https://fcc-weather-api.glitch.me/api/current?",
    format: "json",
    method: "GET",
    data: {
      lon: longitude,
      lat: latitude
    },
    success: function(response) {
      var tempC = Math.floor(response.main.temp);
      var tempF = Math.floor(tempC * 1.8) + 32;
      var description = response.weather[0].description;

      $("#locale").html(response.name);

      $("#icon").html("<img src=" + response.weather[0].icon + ">");

      $("#temperature").html("<h3>The temperature is "+ tempC + "&deg;C with " + description + "</h3>");

      $("#button").html("<button id='toggleTemp' onclick='this.blur()'>toggle &deg;F/&deg;C</button>");

      toggleTemp(tempC, tempF, description);

      console.log(response);
    }
  });
}

function toggleTemp(tempC, tempF, description) {
  var tempInC = true;

  $("#toggleTemp").on("click", function() {
    if (tempInC) {
      $("#temperature").html("<h3>The temperature is " + tempF + "&deg;F with " + description + "</h3>");
      tempInC = false;
    } else {
      $("#temperature").html("<h3>The temperature is " + tempC + "&deg;C with " + description +"</h3>");
      tempInC = true;
    }
  });
}

getLocation();

/***** NOTES *****
Fahrenheit to Celsius conversions:
C to F: Multiply by 1.8 (or 9/5) and add 32.
F to C: Subtract 32 and multiply by .5556 (or 5/9).

jQuery.param() can be used to construct query string:
 http://www.tothenew.com/blog/jquery-create-url-query-string-from-jsonarray/
*/

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console