Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
117 lines (106 loc) · 3.98 KB
/
Copy pathscript.js
File metadata and controls
117 lines (106 loc) · 3.98 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
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
const svgTurkeyMap = document.getElementById("svg-turkey-map").getElementsByTagName("path");
const cardCity = document.getElementById("card__city");
const weather = document.getElementById("weather");
const cityName = document.getElementById("city-name");
const degree = document.getElementById("degree");
const imgElement = document.querySelector("dialog img");
const humidity = document.getElementById("humidity");
const wind = document.getElementById("wind");
const dialogContainer = document.getElementById("dialog__container");
const dialogClose = document.querySelector(".dialog__close");
for (let i = 0; i < svgTurkeyMap.length; i++) {
svgTurkeyMap[i].addEventListener("mousemove", function () {
cityName.classList.add("show-city-name--active");
cityName.style.left = event.clientX + 16 + "px";
cityName.style.top = event.clientY + 16 + "px";
cityName.innerHTML = this.getAttribute("title");
});
svgTurkeyMap[i].addEventListener("mouseleave", function () {
cityName.classList.remove("show-city-name--active");
});
svgTurkeyMap[i].addEventListener("click", function () {
//! API CALL
async function fetchData() {
try {
const response = await fetch(
// &lang=tr Eklenerek dil destekli api alınabilir.
// `https://api.openweathermap.org/data/2.5/weather?q=${cityName.innerHTML}&appid=${apiKey}&lang=tr&units=metric`
`https://api.openweathermap.org/data/2.5/weather?q=${cityName.innerHTML}&appid=${WEATHER_API_KEY}&units=metric`
);
const data = await response.json();
imgElement.src = `./assets/iconWeather/${data.weather[0].icon}.svg`;
cardCity.innerText = data.name.split(" ")[0];
weather.innerText = data.weather[0].description;
degree.innerText = Math.round(data.main.temp) + "°";
humidity.innerText = data.main.humidity;
wind.innerText = data.wind.speed;
} catch (error) {
console.error("Hata:", error);
}
}
fetchData();
dialogContainer.showModal();
});
}
dialogClose.addEventListener("click", function () {
dialogContainer.close();
});
// ! Dialog kartın dışına tıklandığında kart kapanır.
dialogContainer.addEventListener("click", (event) => {
const rect = dialogContainer.getBoundingClientRect();
if (event.clientY < rect.top || event.clientY > rect.bottom || event.clientX < rect.left || event.clientX > rect.right) {
dialogContainer.close();
}
});
var typed = new Typed("#turkey", {
strings: ["Turkey"],
typeSpeed: 200,
startDelay: 500,
showCursor: false,
loop: false,
});
//! LOCATİON
if ("geolocation" in navigator) {
navigator.geolocation.getCurrentPosition(
function (position) {
const latitude = position.coords.latitude;
const longitude = position.coords.longitude;
var requestOptions = {
method: "GET",
};
//! API CALL
async function fetchData() {
try {
const response = await fetch(
`https://api.geoapify.com/v1/geocode/reverse?lat=${latitude}&lon=${longitude}&apiKey=${GEOAPIFY_API_KEY}`,
requestOptions
);
const locationInfo = await response.json();
document.querySelector(".location__text").innerText = locationInfo.features[0].properties.county;
} catch (error) {
console.error("Hata:", error);
}
}
fetchData();
},
function (error) {
// Handle errors, if any
switch (error.code) {
case error.PERMISSION_DENIED:
console.error("User denied the request for geolocation.");
break;
case error.POSITION_UNAVAILABLE:
console.error("Location information is unavailable.");
break;
case error.TIMEOUT:
console.error("The request to get user location timed out.");
break;
case error.UNKNOWN_ERROR:
console.error("An unknown error occurred.");
break;
}
}
);
} else {
console.error("Geolocation is not available in this browser.");
}