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.

            
              <div class="container-fluid">
  <h1 class = 'text-center'>Local Weather App</h1>
  <ul class="weatherLocation"></ul>
  <ul class="weatherInfo"></ul>
</div>
            
          
!
            
              body {
  background-color: black;
}

.container-fluid {
  margin-top: 15%;
  height: 300px;
  border-radius: 1%;
}

ul li {
  list-style-type: none;
}

.text-center {
  font-family: serif;
  font-size: 40px;
  color: white;
}

.btn {
  color: white;
  font-family: serif;
  font-size: 20px;
}
            
          
!
            
              $(document).ready(function() {
  var image = document.createElement('img');
  var imageURL = "https://openweathermap.org/img/w/";
  var imageFormat = ".png";

  var webURL = "https://api.openweathermap.org/data/2.5/weather?";
  var lat;
  var lon;
  var id = "&APPID=ba0a8a5f3fd1703e953e4ae6e2570a93";
  var celsius = "&units=metric";
  var fahrenheit = "&units=imperial";

  //get the latitude and longitude of the location if possible
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(showPosition, showError);
  }
  //otherwise alert that it is not supported by this browser                                             
  else {
    alert("Geolocation is not supported by this browser.");
  }

  //alert a message if one of the erros below occurs
  function showError(error) {
    switch (error.code) {
      case error.PERMISSION_DENIED:
        alert("User denied the request for geolocation");
        break;
      case error.POSITION_UNAVAILABLE:
        alert("Location information is unavailable");
        break;
      case error.TIMEOUT:
        alert("The request to get user location timed out.");
        break;
      case error.UNKNOWN_ERROR:
        alert("An unknown error occured");
        break;
    }
  }

  //show location, temperature and weather conditions
  function showPosition(position) {
    lat = position.coords.latitude;
    lon = position.coords.longitude;
    var rootInKelvin = webURL + "lat=" + lat + "&lon=" + lon + id;
    var rootInCelsius = webURL + "lat=" + lat + "&lon=" + lon + celsius + id;
    var rootInFahrenheit = webURL + "lat=" + lat + "&lon=" + lon + fahrenheit + id;
    //get the city, country and description of the weather
    function locale() {
      $.getJSON(rootInKelvin, function(json) {
        var description = json.weather[0].description;
        var city = json.name;
        var country = json.sys.country;
        var weatherIcon = json.weather[0].icon;
        image.src = imageURL + weatherIcon + imageFormat;
        $('.weatherLocation').append(image);
        $('img').addClass('img-responsive center-block');
        $('.weatherLocation').append('<li>' + '<p class = "text-center">' + city + ', ' + country + '</p>' + '<p class = "text-center">' + description + '</p></li>');
      });
    }
    locale();

    //show the temperature in celcius    
    function inCelsius() {
      $.getJSON(rootInCelsius, function(json) {
        var tempInCelsius = json.main.temp;

        $('.weatherInfo').append('<li><p class = "text-center">' + tempInCelsius + '&#8451; ' + '<br/><button class = "btn btn-primary" id = "convertToFahrenheit">In Fahrenheit</button><button class = "hidden btn btn-primary" id = "convertToCelsius">In Celsius</button><p></li>');
      });
    }
    inCelsius();

    //show the temperature in fahrenheit
    function inFahrenheit() {
      $.getJSON(rootInFahrenheit, function(json) {

        var tempInFahrenheit = json.main.temp;
        $('.weatherInfo').append('<li><p class = "text-center">' + tempInFahrenheit + '&#8457; ' + '<br/><button class = "hidden btn btn-primary" id = "convertToFahrenheit">In Fahrenheit</button><button class = "btn btn-primary" id = "convertToCelsius">In Celsius</button><p></li>');
      });
    }

    //on click, convert temperature to fahrenheit
    $('.weatherInfo').delegate('#convertToFahrenheit', 'click', function() {
      var $li = $(this).closest('li');
      inFahrenheit();
      $li.remove();
    });
    //on click, convert temperature to celcius
    $('.weatherInfo').delegate('#convertToCelsius', 'click', function() {
      var $li = $(this).closest('li');
      inCelsius();
      $li.remove();
    });
  }
});
            
          
!
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