-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
56 lines (42 loc) · 1.67 KB
/
app.js
File metadata and controls
56 lines (42 loc) · 1.67 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
function initAutocomplete() {
const input = document.getElementById("pac-input");
const options = {
types: ["(cities)"],
};
const autocomplete = new google.maps.places.Autocomplete(
input,
options
);
autocomplete.addListener("place_changed", () => {
console.log("test");
const place = autocomplete.getPlace();
if (!place.geometry || !place.geometry.location) {
// User entered the name of a Place that was not suggested and
// pressed the Enter key, or the Place Details request failed.
window.alert(
"No details available for input: '" + place.name + "'"
);
return;
}
const lat = place.geometry.location.lat();
const lng = place.geometry.location.lng();
getWeatherData(lat, lng);
});
async function getWeatherData(lat, lng) {
//build API url for weather data
const url = new URL("https://api.open-meteo.com/v1/forecast?");
url.searchParams.append("latitude", lat);
url.searchParams.append("longitude", lng);
url.searchParams.append("temperature_unit","fahrenheit");
url.searchParams.append("current_weather","true");
//fetch data
const response = await fetch(url);
const data = await response.json();
dataObj = data;
console.log(dataObj);
const temperature = document.getElementById("temp");
temperature.innerText = dataObj.current_weather.temperature;
const windspeed = document.getElementById("wind");
windspeed.innerText = dataObj.current_weather.windspeed;
}
}