-
Notifications
You must be signed in to change notification settings - Fork 0
/
heatmap.html
47 lines (42 loc) · 1.99 KB
/
heatmap.html
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
<!DOCTYPE html>
<html>
<head>
<title>Leaflet Heatmap from API</title>
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" />
<script src="https://unpkg.com/[email protected]/dist/leaflet.js"></script>
<script src="https://cdn.jsdelivr.net/gh/Leaflet/Leaflet.heat@gh-pages/dist/leaflet-heat.js"></script>
</head>
<body>
<div id="map" style="height: 600px; width: 100%;"></div>
<script>
// Initialize the map
var map = L.map('map').setView([0.31038644444444446, 32.58894933333333], 13);
// Add a tile layer to the map
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
// Function to fetch data from the API and render the heatmap
function fetchDataAndRenderHeatmap() {
// Replace {SECRET_TOKEN} with your actual token
var url = 'https://platform.airqo.net/api/v2/predict/heatmap?token={SECRET_TOKEN}';
fetch(url)
.then(response => response.json())
.then(data => {
if (data.success) {
var features = data.predictions.features;
var heatmapData = features.map(function(feature) {
return [feature.geometry.coordinates[1], feature.geometry.coordinates[0], feature.properties.pm2_5];
});
// Add the heatmap to the map
var heat = L.heatLayer(heatmapData, {radius: 25}).addTo(map);
} else {
console.error('Failed to fetch data:', data.message);
}
})
.catch(error => console.error('Error fetching data:', error));
}
// Call the function to fetch data and render the heatmap
fetchDataAndRenderHeatmap();
</script>
</body>
</html>