-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathweathermap.js
46 lines (39 loc) · 1.15 KB
/
weathermap.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
let map;
let weatherLayer;
function initMap(lat, lon) {
map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
})
],
view: new ol.View({
center: ol.proj.fromLonLat([lon, lat]),
zoom: 6
})
});
updateWeatherLayer('temp');
document.getElementById('map-layer').addEventListener('change', function() {
updateWeatherLayer(this.value);
});
}
function updateWeatherLayer(layerType) {
if (weatherLayer) {
map.removeLayer(weatherLayer);
}
weatherLayer = new ol.layer.Tile({
source: new ol.source.XYZ({
url: `https://tile.openweathermap.org/map/${layerType}/{z}/{x}/{y}.png?appid=${apiKey}`,
attributions: ['Weather data © OpenWeatherMap']
})
});
map.addLayer(weatherLayer);
}
// Call this function when the weather data is loaded
function loadWeatherMap() {
const weatherData = JSON.parse(sessionStorage.getItem("weatherData"));
if (weatherData && weatherData.coord) {
initMap(weatherData.coord.lat, weatherData.coord.lon);
}
}