cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

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.

            
              
<div class="container-fluid central">
  <div class = "row text-center">
    <h2>Weather App</h2>
  </div>
  <div class = "row text-center">
    <div class = "col-xs-12 well message">
      The message will go here
    </div>
    <p id="demo"></p>

  </div>
  <div class = "row text-center">
    <div class = "col-xs-12">
      <button id="toggle" class="btn btn-primary">Switch to Fahrenheit</button>
    </div>
  </div>
</div>
            
          
!
            
                var x = "celcius";
  $(document).ready(function() {

    //jquery on click then this to get coordinates https://www.w3schools.com/html/html5_geolocation.asp

    if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(showPosition, giveError, {
        timeout: 10000
      });
    } else {
      $(".message").html("Geolocation is not supported by this browser.");
    }

    //weather API
    function showPosition(position) {

      var lat = position.coords.latitude;
      var long = position.coords.longitude;
      //set here wether it's in celcius or fahrenheit

      var unit = "metric";

      var weatherCall = "http://api.openweathermap.org/data/2.5/weather?lat=" + lat + "&lon=" + long + "&APPID=2a37ac5e69da1b0a5b94a969c6e7186d&units=" + unit;

      $.getJSON(weatherCall, function(json) {
          //getting the json back. Got to clean it up and make it look nice now.
          console.log(json);
          //display the temprature

          $(".message").html(" You're in <b>" + json.name + "</b>, <b>" + json.sys["country"] + "</b></br> The Temperature is <b><span id=\"temperature\">" + json.main["temp"] + " </span></b>");

          //set the background depending on the temperature document.body.style.backgroundImage = "url('img_tree.png')";

          var backgroundTemp = json.main["temp"];

          if (backgroundTemp <= -2) {

            document.body.style.backgroundImage = "url('http://cdn1.theodysseyonline.com/files/2015/12/04/635848557150633136-120303261_winter.jpg')";
          } else if (backgroundTemp > -2 || backgroundTemp < 10) {

            $('body').css({'background-image' : 'url(\'http://misracing.com/site-assets/2015/06/RAIN.jpg\')',
      'background-repeat': 'no-repeat'});
          }

         else {
  document.body.style.backgroundImage = "url('http://terrylascola.com/wp-content/uploads/2014/04/music-mon-rainy-day-mix.jpeg')";
        
        }

      });

  }

  //give error

  function giveError() {

    $(".message").html("Yo navigator aint workin fool");
  }

  });

  //function get weather sits outside the page load so that i can call it again when needed

  $("#toggle").click(function() {
    if (x === "fahrenheit") {
      var temperature = $("#temperature").html();
      temperature = (temperature - 32) * 5 / 9;

      $("#temperature").html(temperature.toFixed(2));
      $("#toggle").html("Switch to Fahrenheit");
      x = "celcius";

    } else {
      x = "fahrenheit";
      var temperature = $("#temperature").html();
      temperature = (temperature * 9 / 5) + 32;
      $("#temperature").html(temperature.toFixed(2));

      $("#toggle").html("Switch to Celcius");

    }

  });
            
          
!
999px
Loading ..................

Console