-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
131 lines (124 loc) · 5.47 KB
/
index.html
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="icon" href="https://cdn.jim-nielsen.com/ios/512/weather-2021-12-07.png?rf=1024">
<title>Weather App</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #bac2df;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 10;
}
.weather-app {
background: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
text-align: center;
}
#weather-result {
margin-top: 20px;
}
p b {
padding: 2px;
}
</style>
</head>
<body>
<div class="weather-app">
<h1>Weather App</h1>
<hr>
<p id="status">Getting your location...</p>
<div id="weather-result">
</div>
</div>
<script>
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.watchPosition(showPosition, showError, { enableHighAccuracy: true });
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
const lat = position.coords.latitude;
const long = position.coords.longitude;
getWeather(lat,long);
}
function showError(error) {
switch(error.code) {
case error.PERMISSION_DENIED:
document.getElementById("status").innerHTML = "User denied the request for Geolocation.";
break;
case error.POSITION_UNAVAILABLE:
document.getElementById("status").innerHTML = "Location information is unavailable.";
break;
case error.TIMEOUT:
document.getElementById("status").innerHTML = "The request to get user location timed out.";
break;
case error.UNKNOWN_ERROR:
document.getElementById("status").innerHTML = "An unknown error occurred.";
break;
}
}
async function getWeather(lat,lon) {
const apiKey = "7660a0cb670a400598d172712241307";
const url = `https://api.weatherapi.com/v1/current.json?key=${apiKey}&q=${lat},${lon}`;
try {
const response = await fetch(url);
const data = await response.json();
if (response.ok) {
displayWeather(data,lat,lon);
} else {
document.getElementById('weather-result').innerHTML = `<p>${data.error.message}</p>`;
}
} catch (error) {
console.error('Error fetching weather data:', error);
document.getElementById('weather-result').innerHTML = `<p>Error fetching weather data</p>`;
}
}
function displayWeather(data,lat,lon) {
const weatherContainer = document.getElementById('weather-result');
let old_date = data.current.last_updated;
let year = old_date.slice(0,4);
let month = old_date.slice(5,7);
let date = old_date.slice(8,10);
let hour = old_date.slice(11,13);
let minute = old_date.slice(14,17);
var anot = 'AM';
if (hour>12){
hour -= 12;
anot = 'PM';
}
const new_date = date +'-'+ month +'-' + year + ' '+hour +':'+minute+' '+anot;
const weatherInfo = `
<h2>${data.location.name}, ${data.location.region}, ${data.location.country}</h2>
<p><b>Local Time:</b> ${new_date}</p>
<p><b>Latitude:</b> <u>${lat}</u></p>
<p><b>Longitude:</b> <u>${lon}</u></p>
<p><b>Temperature:</b> ${data.current.temp_c} °C / ${data.current.temp_f} °F</p>
<p><b>Condition:</b> ${data.current.condition.text}</p>
<img src="${data.current.condition.icon}" alt="${data.current.condition.text} height='60px' width='60px' ">
<p><b>Humidity:</b> ${data.current.humidity}%</p>
<p><b>Wind:</b> ${data.current.wind_kph} kph / ${data.current.wind_mph} mph (${data.current.wind_dir})</p>
<p><b>Pressure:</b> ${data.current.pressure_mb} mb / ${data.current.pressure_in} in</p>
<p><b>Precipitation:</b> ${data.current.precip_mm} mm / ${data.current.precip_in} in</p>
<p><b>Cloud Cover:</b> ${data.current.cloud}%</p>
<p><b>Feels Like:</b> ${data.current.feelslike_c} °C / ${data.current.feelslike_f} °F</p>
<p><b>Visibility:</b> ${data.current.vis_km} km / ${data.current.vis_miles} miles</p>
<p><b>UV Index:</b> ${data.current.uv}</p>
<p><b>Gust:</b> ${data.current.gust_kph} kph / ${data.current.gust_mph} mph</p>
`;
weatherContainer.innerHTML = weatherInfo;
document.getElementById("status").innerHTML = "";
}
getLocation();
</script>
</body>
</html>