css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

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.

            
              

<head></head>

<body>

  <!-- Heading -->
  <div id="title" class="text-center text-primary text-center"> Your Local Weather </div>

  <!-- CONTAINER FOR ICON AND TEMPERATURE  -->
  <div class="container">
    <div id="iconTemp" class="col-xs-4 col-xs-offset-4 well">
      icon temp
    </div>
  </div>

  <!-- CONTAINER FOR LOCATION, WEATHER DESCRIPTION AND WIND SPEED  -->
  <div class="container">

    <!-- WELL FOR LOCATION  -->
    <div id="location" class="col-xs-4 well">location not emptied</div>

    <!-- WELL FOR WEATHER DESCRIPTION -->
    <div id="weatherDescription" class="col-xs-4 well">weatherDescription</div>

    <!-- WELL FOR WIND SPEED -->
    <div id="windSpeed" class="col-xs-4 well">windSpeed</div>
  </div>

</body>
            
          
!
            
              body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  background: url("https://i.imgur.com/SYY2mJz.png") no-repeat;
  background-size: cover;
}

#title {
  font-family: VT323;
  font-size: 45px;
  color: grey;
}

            
          
!
            
              /* weather api key: 	 
14fa2f58b2157183bfc416f78fc183d3
*/

//FUNCTIONS FIRST
console.log("START");
//This function uses ip data to get city, region and units
  function getLocation() { //Define getLocation
    /*Pull in ipinfo and store in ipData object*/
    $.get("https://ipinfo.io", function(ipData) {
      console.log(ipData);
      /* empty and append location well with city and region values parsed through the ipData object */
      $('#location')
        .empty()
        .append(ipData.city + ", ")
        .append(ipData.country);
      /* Store units in variable 'units' using the getUnits function declared below */
      units = getUnits(ipData.country);
      /* Declare the getUnits function, typed below */
      getWeather(ipData.loc, units);
    }, "jsonp"); // Close $.get request
  }


  //This function uses an array of countries using the Imperial system to determine whether users location uses imperial 
  function getUnits(country) {
    /* Create an array of countries that use imerial units */
    var imperialCountries = ['US', 'BS', 'BZ', 'KY', 'PW'];
    if ($.inArray(country, imperialCountries) == -1) {
      var unitsMI = 'metric';
    } else {
      unitsMI = 'imperial';
    }
    console.log(country, unitsMI);
    return unitsMI;
  }

  //This function gets weather info by parsing lat and lon into openweathermap api
  function getWeather(loc, units) {
    lat = loc.split(",")[0]
    lon = loc.split(",")[1]
    openWeatherAPI = 'https://api.openweathermap.org/data/2.5/weather?lat=' + lat + '&lon=' + lon + "&units=" + units + '&appid=14fa2f58b2157183bfc416f78fc183d3';
  console.log(openWeatherAPI);
    
 $.get(openWeatherAPI, function(weatherObj) {
      console.log(weatherObj);
      windDirection = convertWindDirection(weatherObj.wind.deg);
    // console.log("WINDDIRECTIONHERE");
  console.log(weatherObj.wind.deg);
      var temperature = weatherObj.main.temp;
      if (units === "imperial") {
        unitLabel = "F";
      } else {
        unitLabel = "oC";
      }
  // console.log("UNIT LABEL IS " + unitLabel) --> unitLabel works
      temperature = parseFloat((temperature).toFixed(1)) + " " + unitLabel;
   
      //console.log("WITHIN>>>>>>>>>>"+weather);
      $('#weatherDescription')
        .empty()
        .append(weatherObj.weather[0].description);
     /* console.log("WEATHER DESCRIPTION>>>>>>" + 
weatherObj.weather[0].description); //spent a long time without a zero reference! Argh! */
      console.log(weatherObj.wind.speed);
      $('#windSpeed')
        .empty()
        .append(windDirection + ", " + weatherObj.wind.speed + " knots");
   
console.log("temp:"+temperature);
var html = '<img src="http://openweathermap.org/img/w/' + weatherObj.weather[0].icon + '.png" alt="Weather Icon">'  
      $('#iconTemp')
        .empty()
        .append(temperature + html);
console.log("temp:"+temperature);
    }, "jsonp");
  };

//This function converts wind direction from degree's to compass directions
  function convertWindDirection(dir) {
    var rose = ['N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW', 'N'];
    var eightPoint = Math.floor(dir / 45);
    // console.log("COMPASS POINT IS>>>" + rose[eightPoint]); 
    //This was originally evaluating 8 because dir value was 360 and rose array was only 7 values long! Luckily the wind direction today was perfect for spotting the problem of a value of 360! I've added a 'N' at the end of the array now for those days!!!
    return rose[eightPoint];
  }

//-_-_-_-_START_-_-_-_-//
//3..
//2..
//1..
$(document).ready(function() {

  getLocation(); //call getLocation


}); //close doc ready function

            
          
!
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.
Loading ..................

Console