-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
111 lines (92 loc) · 3.6 KB
/
script.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
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
console.log('hello');
// api key : 92efafae85345bebbd9c72b2763f235c
const iconElement = document.querySelector('.weather-icons');
const tempElement = document.querySelector('.temperature-value p');
const descElement = document.querySelector('.temperature-description p');
const locationElement = document.querySelector('.location p');
const notificationElement = document.querySelector('.notification');
const maxTemp = document.querySelector('.maxtemp-value');
const minTemp = document.querySelector('.mintemp-value');
//Data
const weather = {
}
weather.temperature = {
unit: "celsius"
}
/*
weather.maxTemp = {
unit : "celsius"
}
weather.minTemp = {
unit : "celsius"
}*/
// const and var
const KELVIN = 273;
//api key
const key = "92efafae85345bebbd9c72b2763f235c";
//check if brouser supports geonavigation
if ('geolocation' in navigator) {
navigator.geolocation.getCurrentPosition(setPosition, showError);
} else {
notificationElement.style.display = 'block';
notificationElement.innerHTML = "<p>Brouser Doesn't support geolocation</p>";
}
//set user position
function setPosition(position) {
let latitude = position.coords.latitude;
let longitude = position.coords.longitude;
getWeather(latitude, longitude);
}
//show erroe message when there is an issue with the geolocation service
function showError(error) {
notificationElement.style.display = 'block';
notificationElement.innerHTML = `<p> ${error.message} </p>`
}
//get the weather data from api
function getWeather(latitude, longitude) {
let api = `https://api.openweathermap.org/data/2.5/weather?lat=${latitude}&lon=${longitude}&appid=${key}`;
fetch(api)
.then(function (response) {
let data = response.json();
return data;
})
.then(function (data) {
weather.temperature.value = Math.floor(data.main.temp - KELVIN);
/*
weather.maxTemp.value = (data.main.temp_max - KELVIN);
weather.minTemp.value = Math.floor(data.main.temp_min - KELVIN);*/
weather.description = data.weather[0].description;
weather.iconId = data.weather[0].icon;
weather.city = data.name;
weather.country = data.sys.country;
})
.then(function () {
displayWeather()
})
}
//displayWeather to my website
function displayWeather() {
iconElement.innerHTML = `<img src="/icons/${weather.iconId}.png"/>`;
tempElement.innerHTML = `${weather.temperature.value}°<span>C</span>`;
/* maxTemp.innerHTML = `<p><span>Max temperature = </span> ${weather.maxTemp.value}°<span>C</span></p>`;
minTemp.innerHTML = `<p><span>Min temperature = </span> ${weather.minTemp.value}°<span>C</span></p>`;*/
descElement.innerHTML = weather.description;
locationElement.innerHTML = `${weather.city} , ${weather.country} `
}
//celsius to farhenite
function celsiusToFahrenheit(temperature) {
return (temperature * 9/5) + 32;
}
//when user lick on temperature
tempElement.addEventListener("click", function(){
if(weather.temperature.value === undefined) return;
if(weather.temperature.unit == "celsius") {
let farhenite = celsiusToFahrenheit(weather.temperature.value);
farhenite = Math.floor(farhenite);
tempElement.innerHTML = `${farhenite}°<span>F</span>`;
weather.temperature.unit = "farhenite";
} else {
tempElement.innerHTML = `${weather.temperature.value}°<span>C</span>`;
weather.temperature.unit = "celsius";
}
});