-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
53 lines (50 loc) · 1.86 KB
/
Copy pathscript.js
File metadata and controls
53 lines (50 loc) · 1.86 KB
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
const apiKey = "9eb289e073eb2a948de604984e746705";
function fetchWeather(lat, lon) {
const url = `https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&units=metric&appid=${apiKey}`;
fetch(url)
.then(response => {
if (!response.ok) {
throw new Error(`Error: ${response.status} ${response.statusText}`);
}
return response.json();
})
.then(data => {
const weatherInfo = `
<h2>${data.name}</h2>
<p>${data.weather[0].description}</p>
<p>Temperature: ${data.main.temp} °C</p>
`;
document.getElementById("weatherInfo").innerHTML = weatherInfo;
})
.catch(error => {
document.getElementById("weatherInfo").innerHTML = `Unable to fetch weather data. ${error.message}`;
console.error("Error fetching weather:", error);
});
}
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
position => {
const lat = position.coords.latitude;
const lon = position.coords.longitude;
fetchWeather(lat, lon);
},
error => {
if (error.code === error.PERMISSION_DENIED) {
document.getElementById("weatherInfo").innerHTML = "Location access denied. Please allow location access.";
} else {
document.getElementById("weatherInfo").innerHTML = "Unable to get location. Please try again.";
}
console.error("Geolocation Error:", error);
},
{
enableHighAccuracy: true, // Request more accurate location
timeout: 5000, // Timeout after 5 seconds
maximumAge: 0 // Do not use cached location
}
);
} else {
document.getElementById("weatherInfo").innerHTML = "Geolocation is not supported by this browser.";
}
}
window.onload = getLocation;