-
Notifications
You must be signed in to change notification settings - Fork 0
/
default_location_temp.js
41 lines (34 loc) · 1.63 KB
/
default_location_temp.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
const searchBtn = document.getElementById('searchBtn');
const input = document.getElementById('searchbox');
input.addEventListener('keypress', function (e) {
if (e.key === 'Enter') {
searchBtn.click();
}
});
function getCurrentWeatherData() {
navigator.geolocation.getCurrentPosition((success) => {
let { latitude, longitude } = success.coords;
fetch(`https://api.openweathermap.org/data/2.5/onecall?lat=${latitude}&lon=${longitude}&exclude=hourly,minutely&appid=${weather}`)
.then(resp => resp.json())
.then(data => deafultWeatherlog(data));
})
}
getCurrentWeatherData()
const deafultWeatherlog = (data) => {
const getWeatherlogContainer = document.getElementById('weatherResultContainer')
const tempInKelving = data.current.temp
const tempInCelsius = tempInKelving - 273.15
const feelsLikeInKelv = data.current.feels_like
const feelsLikeInCelsius = parseInt(feelsLikeInKelv - 273.15)
const displayCurrentWeatherlog = document.createElement('div')
displayCurrentWeatherlog.classList = 'card-body'
displayCurrentWeatherlog.innerHTML = `
<h5 id="currentLoc"class="card-title fs-3">${data.timezone}, ${data.current.weather[0].main}</h5>
<p class="card-text fs-4">Current Temperature <span class ="fw-bold">${parseInt(tempInCelsius)}℃</span></p>
<p class="card-text fs-4">Feels like
<span class="fw-bold">${feelsLikeInCelsius}℃ </span>
<span class="text-info">|</span> Humidity ${data.current.humidity}
</p>
`
getWeatherlogContainer.appendChild(displayCurrentWeatherlog)
}