-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
53 lines (48 loc) · 1.91 KB
/
Copy pathindex.js
File metadata and controls
53 lines (48 loc) · 1.91 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
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition, showError);
} else {
alert("Geolocația nu este suportată de acest browser.");
}
}
function showPosition(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
// Apel către API-ul OpenWeatherMap pentru a obține datele meteo
fetch(`https://api.openweathermap.org/data/2.5/weather?lat=${latitude}&lon=${longitude}&appid=ee2237d48ca0d5de81a398027162d9a8&units=metric`)
.then(response => response.json())
.then(data => {
var city = data.name;
var temperature = data.main.temp;
var description = data.weather[0].description;
var weatherElement = document.querySelector(".textMark");
var locationElement = document.querySelector(".textMark");
weatherElement.innerHTML = city
var weatherElement = document.querySelector(".showtemp");
var locationElement = document.querySelector(".Showtemp");
weatherElement.innerHTML = temperature
})
.catch(error => {
console.error("Eroare: ", error);
});
}
function showError(error) {
switch(error.code) {
case error.PERMISSION_DENIED:
alert("Permisiunea de geolocație a fost refuzată de utilizator.");
break;
case error.POSITION_UNAVAILABLE:
alert("Informațiile de locație nu sunt disponibile.");
break;
case error.TIMEOUT:
alert("Solicitarea de geolocație a expirat.");
break;
case error.UNKNOWN_ERROR:
alert("A apărut o eroare necunoscută.");
break;
}
}
// Apelarea funcției getLocation() la încărcarea paginii
window.onload = function() {
getLocation();
};