Skip to content

Instantly share code, notes, and snippets.

@bauripalash
Created August 19, 2018 04:50
Show Gist options
  • Save bauripalash/d1175946ba3bad0924bb3401051ba95b to your computer and use it in GitHub Desktop.
Save bauripalash/d1175946ba3bad0924bb3401051ba95b to your computer and use it in GitHub Desktop.
MQBQBo
<p id="error"></p>
<div class="container">
<div class="loader"></div>
<p id="name"></p>
<div class="default">Get Weather</div>
<div class="box">
<div id="temp"></div>
<div id="button-box"><span class="button activated" id="button-f">°F</span><span class="button" id="button-c">°C</span></div>
</div>
<div class="box">
<p id="icon"></p>
<p id="summary"></p>
</div>
</div><small>
<pre><small>This App Is Powered by <a href="http://forcast.io" target="_blank">DarkSky</a>, <a href="http://geocoder.ca" target="_blank">Geocoder.ca </a>
and <a href="https://erikflowers.github.io/weather-icons/" target="_blank">Weather Icons</a></small></pre></small>
<script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js'></script>
<script src="js/index.js"></script>
(function() {
$(document).ready(function() {
var changeTemp, getLocationAndWeather, lat, lon, temp;
lat = void 0;
lon = void 0;
temp = void 0;
getLocationAndWeather = function() {
var getWeather, showError;
// If brwoser supports HTML5 geolocation then execute the getWeather function
getWeather = function(position) {
var Forcast_API_KEY, forcastAPI, geocodeAPI;
// Locate nearest city using Geocoder.ca API
geocodeAPI = 'http://geocoder.ca/?latt=' + lat + '&longt=' + lon + '&reverse=1&allna=1&geoit=xml&corner=1&json=1';
$.getJSON(geocodeAPI, function(data) {
$('#name').html('The current weather in ' + data.city + ',&nbsp;' + data.prov);
});
// Using forcast api to get the current weather.
Forcast_API_KEY = '77385117dbbeae0faae9cd33400cae0e';
forcastAPI = 'https://api.darksky.net/forecast/' + Forcast_API_KEY + '/' + lat + ',' + lon + '?callback=?';
// Could only use an ajax request because the getJSON request was causing a cross domain error
$.ajax({
url: forcastAPI,
dataType: 'jsonp',
crossDomain: true,
success: function(data) {
// Return summary
$('#summary').html(data.currently.summary);
// Return temperature
temp = Math.floor(data.currently.temperature);
$('#temp').html(temp + '&deg;F');
//Switch up the weather icons based on the current condition
switch (data.currently.icon) {
case 'clear-day':
$('#icon').html('<i class="wi wi-day-sunny"></i>');
break;
case 'clear-night':
$('#icon').html('<i class="wi wi-night-clear"></i>');
break;
case 'rain':
$('#icon').html('<i class="wi wi-day-rain-mix"></i>');
break;
case 'snow':
$('#icon').html('<i class="wi wi-day-snow"></i>');
break;
case 'sleet':
$('#icon').html('<i class="wi wi-day-sleet"></i>');
break;
case 'wind':
$('#icon').html('<i class="wi wi-day-windy"></i>');
break;
case 'fog':
$('#icon').html('<i class="wi wi-day-fog"></i>');
break;
case 'cloudy':
$('#icon').html('<i class="wi wi-day-cloudy"></i>');
break;
case 'partly-cloudy-day':
$('#icon').html('<i class="wi wi-day-cloudy"></i>');
break;
case 'partly-cloudy-night':
$('#icon').html('<i class="wi wi-night-cloudy"></i>');
break;
default:
$('#icon').html('<i class="wi wi-day-sunny"></i>');
break;
}
$('.loader').fadeOut(500, function() {
$('#name').fadeIn(500);
$('#icon').fadeIn(500);
$('#temp').fadeIn(500);
$('#summary').fadeIn(500);
$('#button-box').fadeIn(500);
});
}
});
};
// Throw errors if location is not defined
showError = function(error) {
switch (error.code) {
case error.PERMISSION_DENIED:
$('.error').html('User denied the request for Geolocation.');
break;
case error.POSITION_UNAVAILABLE:
$('.error').html('Location information is unavailable.');
break;
case error.TIMEOUT:
$('.error').html('The request to get user location times out.');
break;
case error.UNKNOWN_ERROR:
$('.error').html('An unknown error occurred.');
}
};
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition((function(position) {
// Log the latitude and longitude
lat = position.coords.latitude;
lon = position.coords.longitude;
// Only call this function 6 times a day when the browser is open
setInterval(getWeather(), 144000000);
}), showError);
} else {
$('.default').html('Geolocation is not supported by this browser.');
}
};
// Toggle the temperature between Fahrenheit and Celsius
changeTemp = function() {
$('#button-c').click(function() {
$('#button-c').addClass('activated');
$('#button-f').removeClass('activated');
$('#temp').html(Math.floor((temp - 32) * .5555555) + '&deg;C');
});
$('#button-f').click(function() {
$('#button-f').addClass('activated');
$('#button-c').removeClass('activated');
$('#temp').html(temp + '&deg;F');
});
};
getLocationAndWeather();
changeTemp();
});
// ---
// generated by js2coffee 2.1.0
}).call(this);
body {
background-color: rgba(55, 100, 255, 0.6);
}
body .container {
width: 60%;
min-width: 200px;
margin: 0 auto;
font-family: "Open Sans", sans-serif;
font-weight: 300;
padding: 2em;
border-radius: 2.5em;
margin-top: 5em;
text-align: center;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
-ms-flex-wrap: wrap;
flex-wrap: wrap;
}
body .container .default {
display: none;
color: white;
font-size: 2.2em;
}
@media(max-width: 400px) {
body .container .default {
font-size: 1.5em;
}
}
body .container .box {
color: white;
}
body .container p {
margin: 0.5em;
}
body .container #name {
display: none;
color: white;
min-width: 100%;
font-size: 3em;
}
@media(max-width: 600px) {
body .container #name {
font-size: 2em;
}
}
@media(max-width: 400px) {
body .container #name {
font-size: 1.5em;
}
}
body .container #temp {
display: none;
min-width: 250px;
font-size: 8em;
}
@media(max-width: 600px) {
body .container #temp {
font-size: 5em;
}
}
@media(max-width: 400px) {
body .container #temp {
font-size: 3em;
}
}
body .container #button-box {
margin-top: 0.5em;
display: none;
}
body .container #button-box .button {
color: white;
padding: 0.2em 1em;
font-size: 1em;
font-weight: 700;
border: 2px solid white;
border-radius: 1.5em;
margin: 0 0.25em;
-webkit-transition: all ease-in-out 0.5s;
transition: all ease-in-out 0.5s;
}
body .container #button-box .button:hover {
background-color: white;
border: 2x solid white;
color: rgba(51, 153, 255, 0.6);
cursor: pointer;
}
body .container #button-box .button.activated {
background-color: white;
color: rgba(51, 153, 255, 0.6);
border: 2px solid white;
}
body .container #icon {
display: none;
margin-top: 3em;
}
body .container #icon i {
font-size: 7em;
}
@media(max-width: 600px) {
body .container #icon i {
font-size: 5em;
}
}
@media(max-width: 400px) {
body .container #icon i {
font-size: 3em;
}
}
body .container #summary {
display: none;
color: white;
font-size: 2em;
text-transform: capitalize;
}
@media(max-width: 400px) {
body .container #summary {
font-size: 1.5em;
}
}
body pre {
margin: auto;
position: absolute;
top: 0;
left: 0;
bottom: 100;
right: 0;
border: none;
background: none;
font-family: Open Sans;
color: white;
}
body pre a:hover {
text-decoration: none;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment