<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<!------ Include the above in your HEAD tag ---------->
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
</head>
<body>
<section id='itroBackground' class="intro">
<div class="inner">
<div class="content">
<div class="weather-app">
<div class="right">
<div class="top">
<div id="toggleCelsius" class="temperature-celsius"><span id="temperatureCelsius">0</span></div>
<div style="display:none;" id='toggleFahrenheit' class="temperature-fahrenheit"><span id="temperatureFahrenheit">0</span></div>
<div class="location"><span id="loc">Unknown</span></div></div>
<div class="top">
<img id="icon" width="75px" src="http://openweathermap.org/img/w/11d.png" onerror="this.src='http://openweathermap.org/img/w/11d.png'" />
<p id="description"></p>
</div>
<div class="bottom">
<div class="humidity">
<span>Humidity:
<span id="humidity">0</span>%
<span>
/* =========================
GENERAL START
===========================*/
@import url('https://fonts.googleapis.com/css?family=Raleway');
@import url('https://fonts.googleapis.com/css?family=Oswald');
.intro {
height: 100%;
width: 100%;
margin: auto;
display: table;
}
.intro .inner {
display: table-cell;
vertical-align: middle;
width: 100%;
max-width: none;
}
.content {
max-width: 500px;
margin: 0 auto;
text-align: center
}
/* =========================
GENERAL END
===========================*/
/* =========================
WEATHER START
===========================*/
.weather-app {
margin: auto;
width: 150px;
height: 300px;
/*border: 1px solid black;*/
border-radius: 10px;
box-shadow: 10px 10px 50px #888888;
overflow: hidden;
text-align:center;
font-family: "Mono";
background-color:#3D6AA2;
}
.left {
float: left;
/*background: #262626;*/
padding:10px;
width:40%;
height:100%;
color:white;
}
.temperature-celsius {
margin-top:30px;
margin-bottom:5px;
font-size:32px;
font-weight: bold;
width: 150px;
color:wheat;
}
.location {
font-size: 18px;
width:100%;
width:150px;
color:white;
}
.right {
/*float: right;*/
/*width: 60%;*/
height:100%;
}
.top {
height: 100px;
width: 100%;
margin: auto;
/*background: #cec;*/
}
.top img {
margin-top: 15px;
}
.top p {
margin-top: -14px;
position: relative;
text-transform: capitalize;
}
.bottom {
height: 100px;
/*background: #669999;*/
color: white;
font-weight: bold;
}
.humidity {
padding: 8px;
}
#description {
color:white;
}
/* =========================
WEATHER END
===========================*/
(function WeatherApplication(){
this.init =function() {
this.startApp();
};
/* --------------------------
START APP START
--------------------------- */
this.startApp = function(){
var temperatureCelsius = document.getElementById('temperatureCelsius');
var temperatureFahrenheit = document.getElementById('temperatureFahrenheit');
var loc = document.getElementById('loc');
var icon = document.getElementById('icon');
var description = document.getElementById('description');
var humidity = document.getElementById('humidity');
var wind = document.getElementById('wind');
var direction = document.getElementById('direction');
updateByLaLo();
//update(weather);
};
/* --------------------------
START APP END
--------------------------- */
/* --------------------------
UPDATE BY ZIP START
--------------------------- */
this.updateByLaLo = function() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
console.log('Geolocation is not supported by this browser.');
}
function showPosition(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
var url = 'https://fcc-weather-api.glitch.me/api/current?lat='+latitude+'&lon='+longitude;
sendRequest(url);
}
}
/* --------------------------
UPDATE BY ZIP END
--------------------------- */
/* --------------------------
SEND REQUEST START
--------------------------- */
this.sendRequest = function(url) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if(xhttp.readyState == 4 && xhttp.status == 200){
var data = JSON.parse(xhttp.responseText);
var weather = {};
weather.temperatureCelsius =data.main.temp;
weather.temperatureFahrenheit = data.main.temp * 9 / 5 + 32;
weather.loc = data.name;
weather.icon = data.weather[0].icon;
weather.description = data.weather[0].description;
weather.humidity = data.main.humidity;
weather.wind = data.wind.speed;
weather.direction = data.wind.deg;
update(weather);
}else {
console.log('Error: ReadyState:'+xhttp.readyState+' status:'+xhttp.status);
}
};
xhttp.open('GET', url, true);
xhttp.send();
}
/* --------------------------
SEND REQUEST END
--------------------------- */
/* --------------------------
UPDATE START
--------------------------- */
this.update = function(weather) {
temperatureCelsius.innerHTML = weather.temperatureCelsius+ ' ℃';
temperatureFahrenheit.innerHTML = weather.temperatureFahrenheit+ ' ℉';
loc.innerHTML = weather.loc;
icon.src = weather.icon;
description.innerHTML = weather.description;
humidity.innerHTML = weather.humidity;
wind.innerHTML = weather.wind;
direction.innerHTML = degreesToDirection(weather.direction);
fixBackground(weather.description);
};
/* --------------------------
UPDATE END
--------------------------- */
/* --------------------------
FIX BACKGROUND START
--------------------------- */
this.fixBackground = function(backgroundValued){
var backgrouund = document.getElementById("background");
if(backgroundValued.includes('clouds')) {
backgrouund.className = "clouds-background";
}else if(backgroundValued.includes('rain')) {
backgrouund.className = "rain-background";
}else if(backgroundValued.includes('clear')){
backgrouund.className = "clear-background";
}else {
backgrouund.className = "default-background";
}
}
/* --------------------------
FIX BACKGROUND END
--------------------------- */
/* --------------------------
DEGREES TO DITECTION START
--------------------------- */
this.degreesToDirection = function(degrees){
var range = 360/16;
var low = 360 - range/2;
var high = (low + range) % 360;
var angles = ['N', 'NNE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW','NW','NNW'];
for(i in angles) {
low = (low + range) % 360;
high = (high + range) % 360;
if(degrees >= low && degrees < high){
return angles[i];
}
}
return 'N';
}
/* --------------------------
DEGREES TO DITECTION START
--------------------------- */
/* --------------------------
TOGGLE CF START
--------------------------- */
this.toggleCF = function() {
var fahrenheit = document.getElementById('toggleFahrenheit');
var celsius = document.getElementById('toggleCelsius');
if (fahrenheit.style.display === 'none') {
fahrenheit.style.display = 'block';
celsius.style.display = 'none';
} else {
fahrenheit.style.display = 'none';
celsius.style.display = 'block';
}
}
/* --------------------------
TOGGLE CF START
--------------------------- */
this.init();
})();