-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather.js
54 lines (46 loc) · 1.46 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
const COORDS_LS = 'coords';
const API_WEATHER = 'c04fad8e5e16ffab2117b8474d754dc5';
const weatherContainer = document.querySelector('.js_weather');
function getWeather(lat, lng){
fetch(`https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lng}&appid=${API_WEATHER}&units=metric`)
.then(function(response){
return response.json();
})
.then(function(json){
const temperature = json.main.temp;
const locate = json.name;
weatherContainer.innerText = `${temperature}°C ${locate}`;
});
}
function saveCoords(positionObj) {
localStorage.setItem(COORDS_LS, JSON.stringify(positionObj));
}
function GeoSucccessHandler(position){
const latitude = position.coords.latitude;
const longitude = position.coords.longitude;
const positionObj = {
latitude: latitude,
longitude: longitude
}
saveCoords(positionObj);
getWeather(latitude, longitude);
}
function GeoErrorHandler(){
console.log('Ошибка получения координат');
}
function askForCoords(){
navigator.geolocation.getCurrentPosition(GeoSucccessHandler, GeoErrorHandler);
}
function getCoords(){
const coords = localStorage.getItem(COORDS_LS);
if (coords === null){
askForCoords();
}else {
const loadCoords = JSON.parse(coords);
getWeather(loadCoords.latitude, loadCoords.longitude);
}
}
function init() {
getCoords();
}
init();