Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="style.css">
<title>Weather App</title>
</head>
<body>
<h1>Hola Mundo</h1>
<body>
<div class="container">
<h1 class="cityState">Hola Mundo!</h1>
<h1 class="temperature">Temperature</h1>
</div>
</body>
</html>
<script src="weather.js"></script>
</html>
1 change: 1 addition & 0 deletions indexTemp.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

33 changes: 33 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
body {
display: block;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
font-size: 1 rem;
}

h1 {
display: inline;
}

.container {
display: grid;
}
.cityState {
justify-self: start;
padding-left: 100px;
font-size: 2rem;
}

.temperature {
justify-self: end;
padding-right: 100px;
font-size: 3rem;
}
48 changes: 48 additions & 0 deletions weather.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* DOM Elements */

const cityState = document.querySelector(".cityState");
const temperature = document.querySelector(".temperature");


/* Helper Functions */
function convertTemp(temp) {
const celsius = temp -273;
let fahrenheit = Math.floor(celsius * (9/5) + 32);
return fahrenheit;
}


/* Fetch Functions */

// Fetch location using Geolocator as shown in https://www.youtube.com/watch?v=Xd43hZKaUS0&feature=youtu.be
const successfulLookup = async (position) => {
try {
const {latitude, longitude} = position.coords;
const response = await fetch(`https://api.opencagedata.com/geocode/v1/json?q=${latitude}+${longitude}&key=68824d84dff94b6a8921c156b462a3d1`)
const data = await response.json();
const city1 = data.results[0].components.city;
const stateData = data.results[0].components.state_code;
cityState.textContent = `${city1}, ${stateData}`;
console.log(latitude, longitude)
currentTemp(latitude, longitude);
}
catch(error) {
console.log(error);
}
}


// Fetch weather at openweathermap api
const currentTemp = async (latitude, longitude) => {
try {
const temp = await fetch(`https://api.openweathermap.org/data/2.5/onecall?lat=${latitude}&lon=${longitude}&exclude=minutely,hourly&exclude={part}&appid=0312d87f192d54ce3485295a535d403f`);
const data = await temp.json();
console.log(data);
let currTemp = convertTemp(data.current.temp);
temperature.innerHTML = `${currTemp}&#x2109`;
}
catch(error) {
console.log(error);
}
}
navigator.geolocation.getCurrentPosition(successfulLookup, console.log);