-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
213 lines (170 loc) · 8.08 KB
/
script.js
File metadata and controls
213 lines (170 loc) · 8.08 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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
document.addEventListener("DOMContentLoaded", function() {
var map = L.map('map').setView([28.531759, 77.293973], 10);
var marker = null;
// Define the satellite tile layer
var Esri_WorldImagery = L.tileLayer('https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}', {
attribution: 'Tiles © Esri — Source: Esri, i-cubed, USDA, USGS, AEX, GeoEye, Getmapping, Aerogrid, IGN, IGP, UPR-EGP, and the GIS User Community'
});
// Define the OpenStreetMap tile layer
var osm = L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
});
// Define the dark mode tile layer
var Drk_map = L.tileLayer('https://{s}.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}{r}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors © <a href="https://carto.com/attributions">CARTO</a>',
subdomains: 'abcd',
maxZoom: 20
});
googleTerrain = L.tileLayer('http://{s}.google.com/vt?lyrs=p&x={x}&y={y}&z={z}', {
maxZoom: 20,
subdomains: ['mt0', 'mt1', 'mt2', 'mt3']
});
// Add the satellite tile layer to the map as the default
osm.addTo(map);
// Add layer control to switch between satellite, OSM, and dark mode layers
var baseMaps = {
'Satellite': Esri_WorldImagery,
'OpenStreetMap': osm,
'Dark mode': Drk_map,
'Terrain':googleTerrain
};
// Define the India GeoJSON overlay
var indiadata = L.geoJSON(indiaJson, {
style: {
fillColor: '#e6f598',
fillOpacity: 0.5,
color: 'blue',
weight: 2
}
});
var overlayMaps = {
"India": indiadata
};
// Add layer control with base maps and overlay maps
var layerControl = L.control.layers(baseMaps, overlayMaps).addTo(map);
// Add locate control to the map
L.control.locate().addTo(map);
// Zoom to feature when checkbox is clicked
indiadata.on('add', function () {
map.fitBounds(indiadata.getBounds());
});
const apikey = '9c0553fb336b140adfef5a42bcd2d93e'; // Replace with your actual API key
const apiUrl = `https://api.openweathermap.org/data/2.5/weather?units=metric&q=`;
const apiUrlByCoords = `https://api.openweathermap.org/data/2.5/weather?units=metric&lat={lat}&lon={lon}&appid=${apikey}`;
const searchBox = document.querySelector(".search input");
const searchBtn = document.querySelector(".search button");
const weatherIcon = document.querySelector(".weather-icon");
async function checkWeather(city) {
const response = await fetch(apiUrl + city + `&appid=${apikey}`);
if (response.status == 404) {
document.querySelector(".error").style.display = "block";
document.querySelector(".weather").style.display = "none";
} else {
var data = await response.json();
document.querySelector(".city").innerHTML = data.name;
document.querySelector(".temp").innerHTML = Math.round(data.main.temp) + "°C";
document.querySelector(".humidity").innerHTML = data.main.humidity + "%";
document.querySelector(".wind").innerHTML = data.wind.speed + " km/h";
if (data.weather[0].main == "Clouds") {
weatherIcon.src = "/weather-app-img/images/clouds.png";
} else if (data.weather[0].main == "Drizzle") {
weatherIcon.src = "/weather-app-img/images/drizzle.png";
} else if (data.weather[0].main == "Clear") {
weatherIcon.src = "/weather-app-img/images/clear.png";
} else if (data.weather[0].main == "Rain") {
weatherIcon.src = "/weather-app-img/images/rain.png";
} else if (data.weather[0].main == "Mist") {
weatherIcon.src = "/weather-app-img/images/mist.png";
}
document.querySelector(".weather").style.display = "block";
document.querySelector(".error").style.display = "none";
// Update map view and marker
const lat = data.coord.lat;
const lon = data.coord.lon;
updateMap(lat, lon);
}
}
async function checkWeatherByCoords(lat, lon) {
const response = await fetch(apiUrlByCoords.replace('{lat}', lat).replace('{lon}', lon));
if (response.status == 404) {
document.querySelector(".error").style.display = "block";
document.querySelector(".weather").style.display = "none";
} else {
var data = await response.json();
document.querySelector(".city").innerHTML = data.name;
document.querySelector(".temp").innerHTML = Math.round(data.main.temp) + "°C";
document.querySelector(".humidity").innerHTML = data.main.humidity + "%";
document.querySelector(".wind").innerHTML = data.wind.speed + " km/h";
if (data.weather[0].main == "Clouds") {
weatherIcon.src = "/weather-app-img/images/clouds.png";
} else if (data.weather[0].main == "Drizzle") {
weatherIcon.src = "/weather-app-img/images/drizzle.png";
} else if (data.weather[0].main == "Clear") {
weatherIcon.src = "/weather-app-img/images/clear.png";
} else if (data.weather[0].main == "Rain") {
weatherIcon.src = "/weather-app-img/images/rain.png";
} else if (data.weather[0].main == "Mist") {
weatherIcon.src = "/weather-app-img/images/mist.png";
}
document.querySelector(".weather").style.display = "block";
document.querySelector(".error").style.display = "none";
// Update map view and marker
updateMap(lat, lon);
}
}
function updateMap(lat, lon) {
// Clear previous marker
if (marker) {
map.removeLayer(marker);
}
// Update map view and add new marker
map.setView([lat, lon], 10);
marker = L.marker([lat, lon]).addTo(map)
.bindPopup(`${document.querySelector(".city").innerHTML}`)
.openPopup();
// Clear search box
searchBox.value = '';
}
searchBtn.addEventListener("click", () => {
checkWeather(searchBox.value);
});
map.on('locationfound', function(e) {
const lat = e.latitude;
const lon = e.longitude;
// Check weather for the current location
checkWeatherByCoords(lat, lon);
// Clear search box
searchBox.value = '';
});
map.on('locationerror', function() {
alert("Location access denied.");
});
// Content toggling
const homeLink = document.getElementById("home-link");
const contactLink = document.getElementById("contact-link");
const aboutLink = document.getElementById("about-link");
const homeContent = document.getElementById("home-content");
const contactContent = document.getElementById("contact-content");
const aboutContent = document.getElementById("about-content");
function toggleContent(contentElement) {
homeContent.style.display = "none";
contactContent.style.display = "none";
aboutContent.style.display = "none";
contentElement.style.display = "block";
}
homeLink.addEventListener("click", function(e) {
e.preventDefault();
toggleContent(homeContent);
});
contactLink.addEventListener("click", function(e) {
e.preventDefault();
toggleContent(contactContent);
});
aboutLink.addEventListener("click", function(e) {
e.preventDefault();
toggleContent(aboutContent);
});
// Hide contact and about content initially
contactContent.style.display = "none";
aboutContent.style.display = "none";
});