-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
91 lines (75 loc) · 3.09 KB
/
Copy pathscript.js
File metadata and controls
91 lines (75 loc) · 3.09 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
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
async function getWeather() {
// Get the value from the search input field
let searchInput = document.getElementById('search').value;
// Get the weather data section element
const weatherDataSection = document.getElementById("weather-data");
// Ensure weather data section is visible
weatherDataSection.style.display = "block";
// Replace with your actual OpenWeatherMap API key
const apiKey = "YOUR_API_KEY";
// Validate if search input is empty
if (searchInput == "") {
// Display a message in the weather data section for empty input
weatherDataSection.innerHTML = `
<div>
<h2>Empty Input!</h2>
<p>Please enter a valid <u>city name</u>.</p>
</div>
`;
return;
}
// Function to fetch latitude and longitude coordinates via Geocoding API
async function getLonAndLat() {
const countryCode = 1; // Replace with the actual country code you want to use
const geocodeURL = `https://api.openweathermap.org/geo/1.0/direct?q=${searchInput.replace(" ", "%20")},${countryCode}&limit=1&appid=${apiKey}`;
// Fetch geolocation data
const response = await fetch(geocodeURL);
// Check if response is okay
if (!response.ok) {
console.log("Bad response! ", response.status);
return;
}
// Parse response JSON data
const data = await response.json();
// Handle cases where no data is returned
if (data.length == 0) {
console.log("Something went wrong here.¯\\_(ツ)_/¯");
weatherDataSection.innerHTML = `
<div>
<h2>Invalid Input: "${searchInput}"</h2>
<p>Please try again with a valid <u>city name</u>.</p>
</div>
`;
return;
} else {
return data[0]; // Return the first item in the data array
}
}
// Function to fetch weather data using latitude and longitude
async function getWeatherData(lon, lat) {
// Construct the weather API URL
const weatherURL = `https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&appid=${apiKey}`;
// Fetch weather data
const response = await fetch(weatherURL);
// Parse the weather data from the API response
const data = await response.json();
// Display the weather data in the weatherDataSection element
weatherDataSection.style.display = "flex";
weatherDataSection.innerHTML = `
<img src="https://openweathermap.org/img/wn/${data.weather[0].icon}.png"
alt="${data.weather[0].description}"
width="100"
style="border: 2px solid black;">
<div>
<h2>${data.name}</h2>
<p><strong>Temperature:</strong> ${Math.round(data.main.temp - 273.15)}°C</p>
<p><strong>Description:</strong> ${data.weather[0].description}</p>
</div>
`;
}
// Clear the search input field after submitting the search
document.getElementById("search").value = "";
// Get the geolocation data and then fetch the weather data
const geocodeData = await getLonAndLat();
getWeatherData(geocodeData.lon, geocodeData.lat);
}