-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathweather.js
60 lines (53 loc) · 1.5 KB
/
weather.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
weather = document.querySelector(".weather");
const API = "d12da06f5a1ac0d38e9b62c4d2487d8a";
class Weather {
constructor() {}
//method
saveCoords = (coordsObj) => {
localStorage.setItem("coords", JSON.stringify(coordsObj));
};
handleGeoError = () => {
console.log("can't access geo loc");
};
handleGeoSucces = (position) => {
const latitude = position.coords.latitude;
const longitude = position.coords.longitude;
const coordsObj = {
latitude: latitude,
longitude: longitude,
};
this.saveCoords(coordsObj);
};
askForCoordes = () => {
navigator.geolocation.getCurrentPosition(
this.handleGeoSucces,
this.handleGeoError
);
};
loadCoords = () => {
const loadCoords = localStorage.getItem("coords");
if (loadCoords) {
} else {
this.askForCoordes();
}
const parseCoords = JSON.parse(loadCoords);
this.getWeather(parseCoords.latitude, parseCoords.longitude);
};
getWeather = (lat, long) => {
let result = "";
fetch(
`https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${long}&appid=${API}&units=metric`
)
.then(function (response) {
return response.json();
})
.then(function (json) {
const temperature = Math.floor(json.main.temp);
const place = json.name;
const mainweather = json.weather[0].main;
weather.textContent = `${place} ${temperature}℃ ${mainweather}`;
});
};
}
const geo = new Weather();
geo.loadCoords();