-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
155 lines (135 loc) · 6.06 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
const locationInput = document.getElementById('locationInput');
const searchButton = document.getElementById('searchButton');
const suggestionsList = document.getElementById('suggestions');
let autocompleteResults = [];
locationInput.focus();
function createPropertyElement(key, value, parentElement) {
const propertyElement = document.createElement('div');
propertyElement.classList.add('property');
if (key === 'Condition') {
const conditionText = document.createElement('div');
conditionText.innerHTML = `<strong>${key}:</strong> ${value.text}`;
parentElement.appendChild(conditionText);
// const conditionImage = document.createElement('div');
// conditionImage.innerHTML = `<img id="weatherIcon" src="${value.icon}" alt="${value.text}" />`;
// parentElement.appendChild(conditionImage);
} else {
propertyElement.innerHTML = `<strong>${key}:</strong> ${value}`;
parentElement.appendChild(propertyElement);
}
}
function clearData() {
const currentDataElement = document.getElementById('currentData');
currentDataElement.querySelector('.current').innerHTML = '<h5>Weather:</h5>';
currentDataElement.querySelector('.location').innerHTML = '<h5>Location:</h5>';
currentDataElement.querySelector('.wind').innerHTML = '';
currentDataElement.querySelector('.time').innerHTML = '';
currentDataElement.querySelector('.humidity').innerHTML = '';
currentDataElement.querySelector('.condition').innerHTML = '';
currentDataElement.querySelector('.condition-image').innerHTML = '';
}
async function fetchPlaceSuggestions(query) {
const url = `https://google-place-autocomplete-and-place-info.p.rapidapi.com/maps/api/place/autocomplete/json?input=${query}`;
const options = {
method: 'GET',
headers: {
'x-rapidapi-key': '30a8fbf5ccmshe82b868a3d0a85dp15f4aejsne52a84ac119b',
'x-rapidapi-host': 'google-place-autocomplete-and-place-info.p.rapidapi.com'
}
};
try {
const response = await fetch(url, options);
const result = await response.json();
return result.predictions || [];
} catch (error) {
console.error(error);
return [];
}
}
async function fetchData(location) {
const url = `https://weatherapi-com.p.rapidapi.com/current.json?q=${location}`;
const options = {
method: 'GET',
headers: {
'X-RapidAPI-Key': '30a8fbf5ccmshe82b868a3d0a85dp15f4aejsne52a84ac119b',
'X-RapidAPI-Host': 'weatherapi-com.p.rapidapi.com'
}
};
try {
const response = await fetch(url, options);
if (!response.ok) {
throw new Error('Network response was not ok');
}
const result = await response.json();
if (result.error) {
throw new Error(result.error.message);
}
const currentDataElement = document.getElementById('currentData');
const currentElement = currentDataElement.querySelector('.current');
const locationElement = currentDataElement.querySelector('.location');
const windElement = currentDataElement.querySelector('.wind');
const timeElement = currentDataElement.querySelector('.time');
const humidityElement = currentDataElement.querySelector('.humidity');
const conditionElement = currentDataElement.querySelector('.condition');
const conditionImageElement = currentDataElement.querySelector('.condition-image');
clearData();
createPropertyElement('Temperature', `${result.current.temp_c}°C`, currentElement);
createPropertyElement('Location', result.location.name, locationElement);
createPropertyElement('Country', result.location.country, locationElement);
createPropertyElement('Wind Speed', `${result.current.wind_kph} kp/h`, windElement);
createPropertyElement('Local Time', result.location.localtime, timeElement);
createPropertyElement('Humidity', `${result.current.humidity}%`, humidityElement);
createPropertyElement('Condition', result.current.condition, conditionElement, conditionImageElement);
const iconUrl = `https:${result.current.condition.icon}`;
conditionImageElement.innerHTML = `<img id="weatherIcon" src="${iconUrl}" alt="${result.current.condition.text}" />`;
} catch (error) {
alert('Enter a valid place name');
console.error(error);
}
}
locationInput.addEventListener("input", async function(event) {
const query = event.target.value;
if (query.length > 2) {
autocompleteResults = await fetchPlaceSuggestions(query);
suggestionsList.innerHTML = '';
autocompleteResults.forEach(result => {
const option = document.createElement('option');
option.value = result.description;
suggestionsList.appendChild(option);
});
}
});
locationInput.addEventListener("keydown", async function(event) {
if (event.key === 'Enter') {
const selectedOption = suggestionsList.querySelector('option:checked');
if (selectedOption) {
const selectedPlace = autocompleteResults.find(result => result.description === selectedOption.value);
if (selectedPlace) {
fetchPlaceDetails(selectedPlace.place_id)
.then(placeDetails => {
const locationName = placeDetails.name || selectedOption.value;
fetchData(locationName);
})
.catch(error => {
console.error(error);
alert('Error fetching place details');
});
}
} else {
const location = locationInput.value;
if (location) {
fetchData(location);
} else {
alert('Please enter a place name');
}
}
}
});
searchButton.addEventListener("click", function() {
const location = locationInput.value;
if (location) {
fetchData(location);
} else {
alert('Please enter a place name');
}
});