In: Computer Science
Sign up for an API Key to consume OpenWeathermap.org and using their Weather API, using Bootstrap, axios, jQuery create an application that returns weather information for a given Zip Code (see https://openweathermap.org/current#zip), I.e. a form with a zip code input and button that when i click the button it returns the weather information on the DOM.
Even extra points if you handle the loading state so that the user is aware something is loading.
You can use https://getbootstrap.com/docs/4.4/getting-started/introduction/#starter-template as a starting point
CODE :
Index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> Live Weather App Using jQuery (Open Weather API)
</title>
<meta name="viewport" content="width=device-width,
initial-scale=1.0">
<link
href="https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css"
rel="stylesheet">
</head>
<body class="bg-gray-100 leading-normal my-10"
style="background:#edf2f6;">
<div class="flex mb-4">
<div class="w-full text-center">
<h1 class="text-4xl">Live Weather App Using jQuery (Open
Weather API)</h1>
</div>
</div>
<div class="flex items-center justify-center p-8">
<div class="flex mb-4">
<div class="w-full text-center">
<div class="max-w-sm bg-white shadow-lg rounded p-3">
<div class="timeBlock border border-gray-100 my-2 p-2">
<h4>Date and Time</h4>
<span id="date"></span> | <span
id="time"></span>
</div>
<form>
<div class="max-w-sm my-3">
<input id="location" class="w-full h-12 px-3 rounded mb-2
focus:outline-none focus:shadow-outline text-md shadow-md"
type="search" placeholder="Search your location...">
<button type="submit" class="bg-blue-500 hover:bg-blue-700
text-white font-bold py-2 px-4 rounded">Search
Location</button>
</div>
</form>
<div class="w-full text-center">
<div class="justify-center"><img src="img/weather.png"
width="170px" style="margin: 0 auto;" /></div>
<span id="temperature" class="text-6xl"></span><span
id="unit">°C</span>
<div id="status" class="text-2xl"></div>
</div>
<table class="w-full text-center border-collapse">
<!--Border collapse doesn't work on this site yet but it's
available in newer tailwind versions -->
<tbody>
<tr class="hover:bg-grey-lighter">
<td class="py-4 px-6 border-b border-grey-light"><img
width="30px" src="img/wind.png" style="margin: 0 auto;"
/><span class="windSpeed"></span> <span
class="windSpeedUnit">km/h</span></td>
<td class="py-4 px-6 border-b border-grey-light"><img
width="30px" src="img/pressure.png" style="margin: 0 auto;"
/><span class="pressure"></span></td>
</tr>
<tr class="hover:bg-grey-lighter">
<td class="py-4 px-6 border-b border-grey-light"><img
width="30px" src="img/windsock.png" style="margin: 0 auto;"
/><span
class="windDirection"></span></td>
<td class="py-4 px-6 border-b border-grey-light"><img
width="30px" src="img/humidity.png" style="margin: 0 auto;"
/><span class="humidity"></span></td>
</tr>
</tbody>
</table>
<div class="w-full text-center my-3">
<input id="celsius_F" type="checkbox" class="checkbox">
<label for="celsius_F"
class="text-1xl">Celsius</label>
</div>
</div>
</div>
</div>
</div>
<script
src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js'></script>
<script src="script.js"></script>
</body>
</html>
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
script.js:
var unit = "C";
$(".checkbox").prop("checked", "checked");
function getLocation() {
$.get("https://ipapi.co/json", function(data) {
getWeather(data.city);
});
}
function getWeather(city) {
var api =
"https://cors-anywhere.herokuapp.com/http://api.openweathermap.org/data/2.5/weather?q=";
var units = "&units=metric";
var appid = "&APPID=061f24cf3cde2f60644a8240302983f2";
var $http = api + city + units + appid;
$.getJSON($http, function(data) {
console.log(data);
temp = data.main.temp.toFixed(0);
status = data.weather[0].description;
iconId = data.weather[0].id;
pressure = data.main.pressure ? Math.round(data.main.pressure) :
"N/A ";
humidity = data.main.humidity ? Math.round(data.main.humidity) :
"N/A ";
windSpeed = data.wind.speed ? (data.wind.speed * 3.6).toFixed(0) :
"N/A ";
windDirection = data.wind.deg ? data.wind.deg.toFixed(0) : "N/A
";
city = data.name;
country = data.sys.country;
var hours = new Date().getHours();
var dayOrNight = hours > 6 && hours < 22 ? "day" :
"night";
inputTextValue = city + ", " + country;
$("#location").val(inputTextValue);
$(".forecast-status")
.find(".wi")
.addClass("wi-owm-" + dayOrNight + "-" + iconId);
$("#temperature").text(temp);
$("#status").text(status[0].toUpperCase() + status.slice(1));
$(".pressure").text(pressure + " hPa");
$(".humidity").text(humidity + " %");
$(".windSpeed").text(windSpeed);
$(".windDirection").text(
windDirection + "deg " + degToCompass(windDirection)
);
$(".wi-wind").addClass("towards-" + windDirection + "-deg");
changeBackground(iconId);
});
}
function degToCompass(num) {
var val = Math.floor(num / 22.5 + 0.5);
var arr = [
"(N)",
"(NNE)",
"(NE)",
"(ENE)",
"(E)",
"(ESE)",
"(SE)",
"(SSE)",
"(S)",
"(SSW)",
"(SW)",
"(WSW)",
"(W)",
"(WNW)",
"(NW)",
"(NNW)"
];
return arr[val % 16] || "";
}
function celsius_F() {
if (unit == "F") {
unit = "C";
windSpeedUnit = "km/h";
temp = Math.round((temp - 32) * 5 / 9 * 10 / 10);
windSpeed = Math.round(windSpeed * 1.609344 * 10 / 10);
} else if (unit == "C") {
unit = "F";
windSpeedUnit = "mph";
temp = Math.round((temp * (9 / 5) + 32) * 10 / 10);
windSpeed = Math.round(windSpeed * 0.62137119223733 * 10 /
10);
}
$("#temperature").text(temp);
$("#unit").text("°" + unit);
$(".windSpeed").text(windSpeed);
$(".windSpeedUnit").text(windSpeedUnit);
}
function getDate() {
var d = new Date();
var date = d.toLocaleDateString();
$("#date").html(date);
}
function getClock() {
var d = new Date(),
h = d.getHours(),
m = d.getMinutes(),
s = d.getSeconds();
h = checkTime(h);
m = checkTime(m);
s = checkTime(s);
$("#time").text(h + ":" + m + ":" + s);
var t = setTimeout(getClock, 500);
}
function checkTime(i) {
if (i < 10) {
i = "0" + i;
}
return i;
}
getDate();
getClock();
getLocation();
window.onkeyup = keyup;
var inputTextValue;
function keyup(e) {
//setting your input text to the global Javascript Variable for
every key press
inputTextValue = e.target.value;
if (e.keyCode == 13) {
console.log(inputTextValue);
if (~inputTextValue.indexOf(",")) inputTextValue = "";
getWeather(inputTextValue);
}
}
$(document).ready(function() {
$("#location").on("click", function() {
$(this).val("");
inputTextValue = "";
});
$('form').submit(function() {
if (~inputTextValue.indexOf(",")) inputTextValue = "";
getWeather(inputTextValue);
return false;
});
$("#celsius_F").on("change", function() {
celsius_F();
});
$(".beforeText, .forecast-temp").on("click", function() {
$(".checkbox").prop("checked", function(idx, oldProp) {
return !oldProp;
});
celsius_F();
});
});
Thank you...