-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
53 lines (43 loc) · 1.83 KB
/
script.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
47
48
49
50
51
52
let map = L.map('map').setView([39.8283, -98.5795], 4);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
// Create a marker cluster group
let markerClusterGroup = L.markerClusterGroup();
// Create an array to store heatmap data
let heatmapData = [];
// Fetch voter data and add markers
fetch('voters.json')
.then(response => response.json())
.then(data => {
data.forEach(voter => {
let marker = L.marker([voter.latitude, voter.longitude]);
marker.bindPopup(`Voter ID: ${voter.id}<br>Party: ${voter.party}`);
markerClusterGroup.addLayer(marker);
// Add data point for heatmap
heatmapData.push([voter.latitude, voter.longitude, 1]);
});
// Add the marker cluster group to the map
map.addLayer(markerClusterGroup);
// Create and add the heatmap layer
let heatmapLayer = L.heatLayer(heatmapData, {
radius: 20,
blur: 15,
maxZoom: 10 // Adjust this value to set when to switch from heatmap to markers
}).addTo(map);
// Function to toggle between heatmap and markers based on zoom level
function updateMapView() {
let currentZoom = map.getZoom();
if (currentZoom > 10) { // Adjust this value to match maxZoom in heatmapLayer options
map.removeLayer(heatmapLayer);
map.addLayer(markerClusterGroup);
} else {
map.removeLayer(markerClusterGroup);
map.addLayer(heatmapLayer);
}
}
// Listen for zoom events
map.on('zoomend', updateMapView);
// Initial call to set the correct view
updateMapView();
});