<html lang="en">
<head>
<meta charset="utf-8">
<style>
#map {
width: 100%;
height: 800px;
}
.ol-popup {
position: absolute;
background-color: white;
box-shadow: 0 1px 4px rgba(0, 0, 0, 0.2);
padding: 15px;
border-radius: 10px;
border: 1px solid #cccccc;
bottom: 12px;
left: -50px;
min-width: 300px;
}
.ol-popup:after,
.ol-popup:before {
top: 100%;
border: solid transparent;
content: " ";
height: 0;
width: 0;
position: absolute;
pointer-events: none;
}
.ol-popup:after {
border-top-color: white;
border-width: 10px;
left: 48px;
margin-left: -10px;
}
.ol-popup:before {
border-top-color: #cccccc;
border-width: 11px;
left: 48px;
margin-left: -11px;
}
.ol-popup-closer {
text-decoration: none;
position: absolute;
top: 5px;
right: 8px;
}
.ol-popup-closer:after {
content: "✖";
color: #A9A9A9;
}
</style>
<title>GeoMet OpenLayers Example</title>
<meta name="description" content="GeoMet OpenLayers Example">
<meta name="author" content="CCMEP">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/[email protected]/en/v6.15.1/css/ol.css" type="text/css" />
<script src="https://cdn.jsdelivr.net/gh/openlayers/[email protected]/en/v6.15.1/build/ol.js"></script>
</head>
<body>
<div id="map">
</div>
<div id="popup" class="ol-popup">
<a href="#" id="popup-closer" class="ol-popup-closer"></a>
<div id="popup-content"></div>
</div>
</body>
</html>
/**
* Elements that make up the popup.
*/
let container = document.getElementById("popup");
let content = document.getElementById("popup-content");
let closer = document.getElementById("popup-closer");
/**
* Create an overlay to anchor the popup to the map.
*/
let overlay = new ol.Overlay({
element: container,
autoPan: true,
autoPanAnimation: {
duration: 250
}
});
/**
* Add a click handler to hide the popup.
* @return {boolean} Don't follow the href.
*/
closer.onclick = function () {
overlay.setPosition(undefined);
closer.blur();
return false;
};
let layers = [
new ol.layer.Tile({
source: new ol.source.OSM()
}),
new ol.layer.Tile({
opacity: 0.4,
source: new ol.source.TileWMS({
url: "https://geo.weather.gc.ca/geomet",
params: { LAYERS: "GDPS.ETA_TT", TILED: true },
transition: 0
})
})
];
let map = new ol.Map({
target: "map",
layers: layers,
overlays: [overlay],
view: new ol.View({
center: ol.proj.fromLonLat([-97, 57]),
zoom: 0
})
});
map.on("singleclick", function (evt) {
let coordinate = evt.coordinate;
let xy_coordinates = ol.coordinate.toStringXY(
ol.proj.toLonLat(evt.coordinate),
4
);
let viewResolution = map.getView().getResolution();
let wms_source = map.getLayers().item(1).getSource();
let url = wms_source.getFeatureInfoUrl(
coordinate,
viewResolution,
"EPSG:3857",
{ INFO_FORMAT: "application/json" }
);
content.innerHTML = '<p align="center">Chargement...</p>';
overlay.setPosition(evt.coordinate);
if (url) {
fetch(url)
.then(function (response) {
return response.json();
})
.then(function (json) {
content.innerHTML = `
Air surface temperature<br>
Coordinates (Lon/Lat): </> <code>${xy_coordinates}</code><br>
Value: </b><code>${Math.round(json.features[0].properties.value)} °C</code>`;
});
}
});
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.