-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwth.js
More file actions
45 lines (37 loc) · 1.47 KB
/
wth.js
File metadata and controls
45 lines (37 loc) · 1.47 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
const API_KEY = 'a37c9cb110fe3213e5f61c2471df9b86';
const COORDS = 'coords';
const weatherInfo = document.getElementById('weatherInfo');
//const weatherIcon = document.querySelector('.weatherIcon');
const weatherIconImg = document.getElementById('weatherIcon');
function init(){
askForCoords();
}
function askForCoords() {
navigator.geolocation.getCurrentPosition(handleSuccess, handleError);
}
function handleSuccess(position) {
const latitude = position.coords.latitude;
const longitude = position.coords.longitude;
const coordsObj = { latitude, longitude };
getWeather(latitude, longitude);
}
function handleError(){
//document.write("can'not access to location");
console.log("can't not access to location");
}
function getWeather(lat, lon){
fetch(`https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&appid=${API_KEY}&units=metric&lang=en`).then(function(response){
return response.json();
})
.then(function(json) {
const temperature = json.main.temp;
const place = json.name;
const weatherDescription = json.weather[0].description;
const weatherIcon = json.weather[0].icon;
const weatherIconUrl = `http://openweathermap.org/img/wn/${weatherIcon}@2x.png`;
weatherInfo.innerHTML = `${Math.floor(temperature)} °C / @${place} / ${weatherDescription}`;
weatherIconImg.setAttribute('src', weatherIconUrl);
})
.catch((error) => console.log("error", error));
}
init();