FreeCodeCamp - Local Weather

Developer
Size
3,926 Kb
Views
18,216

How do I make an freecodecamp - local weather?

Free Code Camp challenge to display the local weather based on the user's position. I have used Google Maps API and forecast.io API. . What is a freecodecamp - local weather? How do you make a freecodecamp - local weather? This script and codes were developed by Veronika on 30 November 2022, Wednesday.

FreeCodeCamp - Local Weather Previews

FreeCodeCamp - Local Weather - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>FreeCodeCamp - Local Weather </title> <link href='https://fonts.googleapis.com/css?family=Economica' rel='stylesheet' type='text/css'>
<meta name="viewport" content="width=device-width, initial-scale=1"> <link rel='stylesheet prefetch' href='https://cdnjs.cloudflare.com/ajax/libs/weather-icons/2.0.9/css/weather-icons.css'> <link rel="stylesheet" href="css/style.css">
</head>
<body> <body> <div class="container"> <span id="errorMessage"></span> <div class="loader"></div> <div class="inner-container"> <header> <span id="weatherDescription">Mostly Cloudy</span> <i class="fa fa-cog settings-icon" id="settings"></i> <ul class="options"> <li id="farenheit">&degF</li> <li id="celsius">&degC</li> </ul> </header> <div class="weather-icon"> <i id="weatherIcon" class="wi wi-forecast-io-partly-cloudy-night"></i> </div> <div class="temperature-section"> <span id="temperature">18</span><span id="unit">°C</span> </div> <footer> <p id="userLocation">Stockholm, Sverige</p> </footer> </div> </div>
</body> <script src='https://code.jquery.com/jquery-2.2.4.min.js'></script>
<script src='https://use.fontawesome.com/d0c3a87dba.js'></script> <script src="js/index.js"></script>
</body>
</html>

FreeCodeCamp - Local Weather - Script Codes CSS Codes

* { text-transform: uppercase;
}
*,
*:before,
*:after { box-sizing: border-box;
}
p { margin: 0;
}
/** CONTAINERS **/
.container { position: relative; margin: auto; height: 400px; width: 30%; min-width: 300px; border-radius: 10px; background-color: rgb(50, 190, 188); color: white; font-family: 'Economica', sans-serif;
}
.inner-container {
}
.temperature-section { position: absolute; bottom: 60px; left: 10px; right: 10px; padding-left: 10px; font-size: 48px; color: rgb(50, 50, 50);
}
header { position: absolute; left: 10px; right: 10px; top: 0; height: 60px; line-height: 60px; padding-left: 10px; font-size: 28px; z-index: 999;
}
footer { position: absolute; left: 10px; right: 10px; bottom: 0; height: 50px; line-height: 50px; border-top: 1px solid; padding-left: 10px; font-size: 22px;
}
/** ICONS **/
.weather-icon { position: absolute; left: 0; right: 0; top: 100px; text-align: center; font-size: 140px; z-index: 1;
}
.settings-icon { float: right; padding: 10px; margin-top: 2px; font-size: 36px !important; cursor: pointer;
}
/** SETTINGS MENU **/
.options { display: none; position: absolute; top: 60px; right: 0; width: 45%; padding: 0; margin: 0; background-color: rgba(255, 255, 255, 0.7); color: rgb(50, 50, 50); text-align: center; border-radius: 3px;
}
.options li { display: block; width: 100%; cursor: pointer;
}
.options li:hover { background-color: rgba(255, 255, 255, 0.9);
}
/** OTHER **/
#errorMessage { display: block; position: absolute; top: 70px; left: 0; right: 0; text-align: center;
}
.loader { display: none; border: 6px solid rgba(255, 255, 255, 0.5); border-top: 6px solid rgb(255, 255, 255); border-radius: 50%; position: absolute; left: 50%; top: 50%; width: 50px; height: 50px; margin: -25px 0 0 -25px; -webkit-animation: spin 2s linear infinite; animation: spin 2s linear infinite;
}
@-webkit-keyframes spin { 0% { -webkit-transform: rotate(0deg); transform: rotate(0deg); } 100% { -webkit-transform: rotate(360deg); transform: rotate(360deg); }
}
@keyframes spin { 0% { -webkit-transform: rotate(0deg); transform: rotate(0deg); } 100% { -webkit-transform: rotate(360deg); transform: rotate(360deg); }
}

FreeCodeCamp - Local Weather - Script Codes JS Codes

var currentUnit = 'F';
var iconMap = {};
iconMap['clear-day'] = 'wi-forecast-io-clear-day';
iconMap['clear-night'] = 'wi-forecast-io-clear-night';
iconMap['rain'] = 'wi-forecast-io-rain';
iconMap['snow'] = 'wi-forecast-io-snow';
iconMap['sleet'] = 'wi-forecast-io-sleet';
iconMap['wind'] = 'wi-forecast-io-wind';
iconMap['fog'] = 'wi-forecast-io-fog';
iconMap['cloudy'] = 'wi-forecast-io-cloudy';
iconMap['partly-cloudy-day'] = 'wi-forecast-io-partly-cloudy-day';
iconMap['partly-cloudy-night'] = 'wi-forecast-io-partly-cloudy-night';
iconMap['hail'] = 'wi-forecast-io-hail';
iconMap['thunderstorm'] = 'wi-forecast-io-thunderstorm';
iconMap['tornado'] = 'wi-forecast-io-tornado';
function getPosition() { return $.Deferred(deferred => navigator.geolocation.getCurrentPosition(deferred.resolve, deferred.reject));
};
function getWeather(position) { const url = `https://api.forecast.io/forecast/83f436fc1b093e004912f28e87f08d9e/${position.coords.latitude},${position.coords.longitude}?callback=?`; return $.getJSON(url).then((data) => data);
}
function getLocationName(position) { const url = `https://maps.googleapis.com/maps/api/geocode/json?latlng=${position.coords.latitude},${position.coords.longitude}&key=AIzaSyChcQU2s3VYM45zsGpXJ1-IIKRpTv0j-aM`; return $.getJSON(url).then((data) => data);
}
function celsiusToFarenheit(celsius) { return celsius * 1.8 + 32;
}
function farenheitToCelsius(farenheit) { return (farenheit - 32) / 1.8;
}
function convert(fromUnit, toUnit) { var currentVal = $('#temperature').text(); var newVal; if (fromUnit === 'farenheit') { newVal = Math.round(farenheitToCelsius(currentVal)); $('#unit').text('\xB0C'); currentUnit = 'C'; } else { newVal = Math.round(celsiusToFarenheit(currentVal)); $('#unit').text('\xB0F'); currentUnit = 'F'; } $('#temperature').text(newVal);
}
$(document).ready(function() { $('.inner-container').hide(); $('.loader').show(); //FETCH WEATHER getPosition() .then(position => $.when(getWeather(position), getLocationName(position)), function(err) { var codePenURL = 'https://codepen.io/ivhed/pen/pbzwJY'; var errorMessage = `${err.message} Try <a href="${codePenURL}" target="_blank">${codePenURL}</a>.`; $('#errorMessage').html(errorMessage); $('.loader').hide(); }) .then(function(weatherData, locationData) { $('#userLocation').text(locationData.results[3].formatted_address); $('#weatherDescription').text(weatherData.currently.summary); $('#temperature').text(Math.round(weatherData.currently.temperature)); $('#unit').text('\xB0F'); $('#weatherIcon').addClass(iconMap[weatherData.currently.icon]); $('.loader').hide(); $('.inner-container').fadeIn(3000); }); //EVENT LISTENERS $('#settings').click(function() { $('.options').slideToggle(); }); $('body').on('click', '#celsius', function() { $('.options').slideToggle(); if (currentUnit != 'C') { convert('farenheit', 'celsius'); } }); $('body').on('click', '#farenheit', function() { $('.options').slideToggle(); if (currentUnit != 'F') { convert('celsius', 'farenheit'); } });
});
FreeCodeCamp - Local Weather - Script Codes
FreeCodeCamp - Local Weather - Script Codes
Home Page Home
Developer Veronika
Username ivhed
Uploaded November 30, 2022
Rating 3
Size 3,926 Kb
Views 18,216
Do you need developer help for FreeCodeCamp - Local Weather?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Veronika (ivhed) Script Codes
Create amazing video scripts with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!