-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
57 lines (47 loc) · 1.33 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
console.log("Hello");
const API_KEY = "168771779c71f3d64106d8a88376808a";
function renderWeatherInfo(data) {
let newPara = document.createElement("p");
newPara.textContent = `${data?.main?.temp.toFixed(2)} °C`;
document.body.appendChild(newPara);
}
async function showWeather() {
try {
let city = "mandsaur";
const response = await fetch(
`https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${API_KEY}&units=metric`
);
const data = await response.json();
console.log("Weather -> ", data);
renderWeatherInfo(data);
} catch (err) {
console.warn(err);
}
}
async function getCustomWeather() {
try {
let latitude = 24.0667;
let longitude = 75.0667;
let result =
await fetch(`https://api.openweathermap.org/data/2.5/weather?lat=${latitude}&lon=${longitude}&appid=${API_KEY}
`);
let data = await result.json();
console.log("Weather -> ", data);
renderWeatherInfo(data);
} catch (err) {
console.log(err);
}
}
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
console.log("No geolocation Support available");
}
}
function showPosition(position) {
let lat = position.coords.latitude;
let longi = position.coords.longitude;
console.log(lat);
console.log(longi);
}