-
Notifications
You must be signed in to change notification settings - Fork 0
/
google-maps.html
137 lines (125 loc) · 4.75 KB
/
google-maps.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
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
<!DOCTYPE html>
<html>
<head>
<title>Air Quality Map</title>
<!-- Include the Google Maps JavaScript API -->
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_GOOGLE_MAPS_API_KEY&libraries=visualization"></script>
<style>
/* Style the map container */
#map {
height: 400px;
width: 100%;
}
</style>
</head>
<body>
<h1>Air Quality Map</h1>
<!-- Create a container for the map -->
<div id="map"></div>
<script>
// Replace 'YOUR_GOOGLE_MAPS_API_KEY' with your actual Google Maps API key
const googleMapsApiKey = "YOUR_GOOGLE_MAPS_API_KEY";
// Create a map centered at a specific location
const map = new google.maps.Map(document.getElementById("map"), {
center: { lat: -0.091702, lng: 34.767956 }, // Kisumu County, Kenya
zoom: 10, // Adjust the zoom level as needed
});
// Sample coordinates defining the boundary of Kisumu County (replace with actual coordinates)
const kisumuBoundaryCoordinates = [
{ lat: -0.05, lng: 34.75 },
{ lat: -0.05, lng: 34.8 },
{ lat: -0.1, lng: 34.8 },
{ lat: -0.1, lng: 34.75 },
];
// Create a polygon representing the Kisumu County boundary
const kisumuBoundaryPolygon = new google.maps.Polygon({
paths: kisumuBoundaryCoordinates,
});
// Calculate the bounds of the polygon
const bounds = new google.maps.LatLngBounds();
kisumuBoundaryCoordinates.forEach((coord) => {
bounds.extend(coord);
});
// Set the map's viewport to the calculated bounds
map.fitBounds(bounds);
// Fetch air quality data from the AirQo API with the access token as a query parameter
fetch(
"https://api.airqo.net/api/v2/devices/measurements/grids/{grid_ID}?token=YOUR_ACCESS_TOKEN",
{
method: "GET",
}
)
.then((response) => response.json())
.then((data) => {
// Extract and format air quality data from the API response
const airQualityData = data.measurements.map((measurement) => {
return {
location: {
lat: measurement.siteDetails.approximate_latitude,
lng: measurement.siteDetails.approximate_longitude,
},
pm2_5: measurement.pm2_5.value,
aqiColorName: measurement.aqi_color_name,
lastRefreshed: new Date(measurement.time).toLocaleString(),
formattedName: measurement.siteDetails.description,
};
});
// Iterate through the air quality data and create markers on the map
airQualityData.forEach((data) => {
// Set the marker color based on the AQI color name
let markerColor;
switch (data.aqiColorName) {
case "Green":
markerColor = "green";
break;
case "Yellow":
markerColor = "yellow";
break;
case "Red":
markerColor = "red";
break;
case "Purple":
markerColor = "purple";
break;
case "Maroon":
markerColor = "maroon";
break;
default:
markerColor = "yellow"; // Default color for unknown or unhandled cases
}
// Create a marker with the specified color
const marker = new google.maps.Marker({
position: data.location,
map: map,
title: `PM2.5: ${data.pm2_5} µg/m³`,
icon: {
path: google.maps.SymbolPath.CIRCLE,
fillColor: markerColor,
fillOpacity: 1,
strokeWeight: 0,
scale: 10, // Adjust the scale as needed
},
});
// Create an info window to display the details
const infoWindow = new google.maps.InfoWindow({
content: `
<div>
<strong>PM2.5:</strong> ${data.pm2_5} µg/m³<br>
<strong>Last Refreshed:</strong> ${data.lastRefreshed}<br>
<strong>Location:</strong> ${data.formattedName}
</div>
`,
});
// Add event listener to show the info window when the marker is clicked
marker.addListener("click", () => {
infoWindow.open(map, marker);
});
// Customize marker icons, labels, or other properties as needed
});
})
.catch((error) => {
console.error("Error fetching air quality data:", error);
});
</script>
</body>
</html>