Pen Settings

HTML

CSS

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

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="container-fluid">

   <body>
      <div class="vertical-center">
         <div class="container">
            <div id="city" class="col-xs-6 col-xs-offset-3 text-center"></div><br>
            <div id="tempBox" class="col-xs-6 col-xs-offset-3">
               <p class="text-center">
                  <span id="temp"></span>
                  <strong><span id="tempId"></span></strong>
               </p>
            </div><br/>

            <div class="col-xs-6 col-xs-offset-3"><img id="weatherIcon" class="img-responsive" width="120" height="120"></img>
            </div><br/>
            <div id="weatherDesc" class="col-xs-6 col-xs-offset-3 text-center"></div>
         </div>
      </div>
   </body>
</div>
              
            
!

CSS

              
                /* openweathermap: 39127289f84c4b76a04b9b47a08e3d08

*/

body {
   font-size: 4vmax;
   font-family: 'Roboto', sans-serif;
   /*background-color: #CCBAA0;*/
   color: #7F7464;
   animation-name: color-swap;
   animation-duration: 100s;
   animation-timing-function: linear;
   animation-iteration-count: infinite;
}

#weatherIcon {
   margin: 0 auto;
}

.vertical-center {
   min-height: 100%;
   min-height: 100vh;
   display: flex;
   align-items: center;
}

#temp {
   animation-name: blinker;
   animation-duration: 5s;
   animation-timing-function: linear;
   animation-iteration-count: infinite;
}

@keyframes blinker {
   0% {
      opacity: 1.0;
   }
   50% {
      opacity: 0.0;
   }
   100% {
      opacity: 1.0;
   }
}

@keyframes color-swap {
   0% {
      background-color: #000000;
   }
   50% {
      background-color: #ffffff;
   }
   100%{
      background-color: #000000;
   }
}

#tempId:hover {
   color: #7F6846;
}
              
            
!

JS

              
                //please run these code in https domain.
Number.prototype.toRad = function() {
   return this * (Math.PI / 180);
};

function changeCF(temp, current) {
   if (current === 1) {
      return temp * 9 / 5 + 32;
   } else
      return (temp - 32) * 5 / 9;
}

//distances between 2 coords
function calDistance(lon1, lat1, lon2, lat2) {
   var dLat = (lat2 - lat1).toRad();
   var dLon = (lon2 - lon1).toRad();
   lat1 = lat1.toRad();
   lat2 = lat2.toRad();
   var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
      Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2);
   return 6371 * 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
}
$(document).ready(function() {

   var currency = 1;
   var temp, url_OWT, JSONObj;
   var flag = true;
   var lon, lat;
   var intervalCount = 0;
   //initial show

   if (navigator.geolocation) {

      navigator.geolocation.getCurrentPosition(function(pos) {
         lon = pos.coords.longitude;
         lat = pos.coords.latitude;
         url_OWT = "https://crossorigin.me/http://api.openweathermap.org/data/2.5/weather?&appid=39127289f84c4b76a04b9b47a08e3d08&lon=" + lon + "&lat=" + lat;

         $.getJSON(url_OWT, function(json) {
            JSONObj = JSON.parse(JSON.stringify(json));
            temp = parseInt(JSONObj.main.temp) - 273.15;
            $("#temp").fadeIn("slow").html(temp.toFixed(2));
            $("#tempId").fadeIn(1500).html("&#8451;");
            $("#city").html(JSONObj.name);
            $("#weatherDesc").html(JSONObj.weather[0].description);
            $("#weatherIcon").attr("src", "http://openweathermap.org/img/w/" + JSONObj.weather[0].icon + ".png");

            $("#tempId").click(
               //if (flag){
               function() {
                  if (flag) {
                     flag = false;
                     $("#temp").fadeOut(function() {

                        $("#temp").fadeIn("slow").html((temp = changeCF(temp, currency)).toFixed(2));
                        if (currency === 1) {
                           currency *= -1;
                           $("#tempId").html("&#8457;");
                           flag = true;
                        } else {
                           currency *= -1;
                           $("#tempId").html("&#8451;");
                           flag = true;
                        }

                     });

                  }
               });
         });
      });
   }

   setInterval(function() { //interval phase
      intervalCount++;
      if (navigator.geolocation) {
         navigator.geolocation.getCurrentPosition(function(pos) {

            //code for forecast API,
            /*--------------------------------------------------
            var url = "https://crossorigin.me/https://api.forecast.io/forecast/appid/" + pos.coords.latitude + "," + pos.coords.longitude;
            //$("#data1").html(url);
            $.getJSON(url, function(json) {
               var JSONObj = JSON.parse(JSON.stringify(json));
               $("#data2").html(JSONObj.latitude + "  " + JSONObj.longitude);

            });
            --------------------------------------------------*/
            //url for api, hide appid in other var for security purpose, https://crossorigin.me used for crossing codepen and openweathermap domain with each other, use https:// in crossorigin.me for https:// in codepen.io, and http://crossorigin.me for http.
            //openweathermap.org only run in http:// domain.
            if (calDistance(lon, lat, pos.coords.longitude, pos.coords.latitude) > 0.5 || intervalCount === 180) {
               if (calDistance(lon, lat, pos.coords.longitude, pos.coords.latitude) > 0.5) {
                  lon = pos.coords.longitude;
                  lat = pos.coords.latitude;
               }
               url_OWT = "https://crossorigin.me/http://api.openweathermap.org/data/2.5/weather?&appid=39127289f84c4b76a04b9b47a08e3d08&lon=" + lon + "&lat=" + lat;
               $.getJSON(url_OWT, function(json) {
                  JSONObj = JSON.parse(JSON.stringify(json));
                  $("#city").html(JSONObj.name);
                  $("#weatherDesc").html(JSONObj.weather[0].description);
                  $("#weatherIcon").attr("src", "http://openweathermap.org/img/w/" + JSONObj.weather[0].icon + ".png");

               });
               if (intervalCount === 180) {
                  intervalCount = 0;
               }
            }
            //flawed temp update, won't update continuously with interval if user not moving, will only update every 15min or if the app restarted or when moving out of 500meters range 
            if (flag) {
               flag = false;
               if (currency === 1) {
                  temp = parseInt(JSONObj.main.temp) - 273.15;
                  flag = true;
               } else {
                  temp = changeCF(parseInt(JSONObj.main.temp) - 273.15, 1);
                  flag = true;
               }
               $("#temp").html(temp.toFixed(2));
            }
         });

      }
   }, 5000);
});

/* 
   ?-----------------------------------------------------------------------------------------------------?
   Ajax method for getting data ont openweatherAPI, parse using jsonp alternative for getJSON on above
   only use for http domain, wont run on https domain for security blocking level is too high

   Cannot use in conjuntion with the navigator geo-location data originated in devices as geo-location won't work on http domain for security purpose, accidentally contradicting with how openweatherAPI is used

   ip-API was used on behalf of navigator.geolocation for tracking purpose.
   
   update 1: 
   - ip-API only support internet providers's geolocation - prove unuseful for personal locating and weather forecasting purpose, move back to naviator.
   - geolocation for more acurate data. openweatherAPI, as further researched does not support https domain for credit purpose, would require credit charge, as so, moving to use Dark Sky API which provide 1000 free services/day 
   
   update 2:
   - after using CORS via crossorigin.me both openweathermap and darkskyAPI have successfully parsed data through https, and as openweathermap proves more useful for unlimited connection, suitable for the application's main achievement, rather than having vast data amount through a period of time as from DarkSkyAPI, which could be more considerable in future instances, but not for now.
   - change back to openweathermap API.
   - build application fully in https domain, for geolocation's sensibility.
   ?-------------------------------------------------------------------------------------------------------?
//accessing openweathermap through ajax, still need CORS to get through https domain of codepen

   function logResults(json) {
         $("#data").html(json);
      }
      $.ajax({
         url: "https://crossorigin.me/http://api.openweathermap.org/data/2.5/weather?lat=10.7584989&lon=106.6401928&appid=&callback=?",
         dataType: "jsonp",
         success: function(data) {
            console.log(data);
         }
      });
      */
              
            
!
999px

Console