-
Notifications
You must be signed in to change notification settings - Fork 0
/
weatherTracker.js
111 lines (78 loc) · 3.31 KB
/
weatherTracker.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
// import { weatherApiKey } from "./config.js";
// Defining variables for latitude and longitude
var lat;
var lon;
// Adding the Leaflet Map to the HTML element with id "map"
var map = L.map('mapid').setView([0, 0], 7);
// Capturing user input as a location
const query = prompt("Please enter your location");
// Adding the base layer to the map using the ArcGIS service
L.tileLayer(`https://server.arcgisonline.com/ArcGIS/rest/services/Canvas/World_Light_Gray_Base/MapServer/tile/{z}/{y}/{x}`, {
attribution: 'Tiles © Esri — Esri, DeLorme, NAVTEQ',
maxZoom: 16
}).addTo(map);
getWeatherData();
// Async function to retrieve the weather based on the user's query
async function getWeatherData() {
try {
const response = await fetch(`http://127.0.0.1:3000/query?query=${query}`);
const data = await response.json();
var lat = data.lat;
var lon = data.lon;
// Setting the map view to the retrieved latitude and longitude
map.setView([lat, lon], 6);
} catch (error) {
console.error(error);
console.log(error.message);
}
}
// Array of layer types for the map
var layerType = ["clouds", "temp", "precipitation"];
// Array to store the map layer objects
var mapLayers = [
];
// Loop to populate mapLayer objects with information for each layer type
for (var i = 0; i < layerType.length; i++) {
mapLayers.push({});
// Adding the button ID for each layer
mapLayers[i].buttonID = `${layerType[i]}-button`;
// Adding the layer name for each layer
mapLayers[i].layerName = `${layerType[i]}_new`;
// Adding the tile layer for each layer using the OpenWeatherMap API
mapLayers[i].tile = L.tileLayer(`https://tile.openweathermap.org/map/${mapLayers[i]["layerName"]}/{z}/{x}/{y}.png?appid=${process.env.OPENWEATHER_API_KEY}`, {
maxZoom: 19,
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a>'
});
}
// Getting all buttons in the HTML document
var layerBtnDiv = document.getElementById("layer-selector");
var button = layerBtnDiv.getElementsByTagName("button");
// Loop to add click event listeners to each button
for (var i = 0; i < button.length; i++) {
let layer = i;
button[i].addEventListener("click", function () { displayLayer(layer) });
}
// Variable to keep track of the currently active layer
var activeLayer;
// Function to display the selected layer on the map
function displayLayer(layer) {
if (activeLayer == undefined) {
mapLayers[layer].tile.addTo(map);
button[layer].classList.add("active");
button[layer].ariaPressed = true; // WORKING? Maybe just through bootstrap.js
activeLayer = layer;
} else if (layer == activeLayer) {
mapLayers[layer].tile.removeFrom(map);
button[layer].classList.remove("active");
button[layer].ariaPressed = false; // NOT WORKING
activeLayer = undefined;
} else {
mapLayers[activeLayer].tile.removeFrom(map);
button[activeLayer].classList.remove("active");
button[activeLayer].ariaPressed = false; // NOT WORKING
mapLayers[layer].tile.addTo(map);
button[layer].classList.add("active");
button[layer].ariaPressed = true; // WORKING? Maybe just through bootstrap.js
activeLayer = layer;
}
}