Skip to content

Commit a4da7ca

Browse files
committed
Added marker cluster to the map
1 parent c4f5d67 commit a4da7ca

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

frontend/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"leaflet": "^1.9.4",
2121
"leaflet-easybutton": "^2.4.0",
2222
"leaflet-iconmaterial": "^1.1.0",
23+
"leaflet.markercluster": "^1.5.3",
2324
"papaparse": "^5.4.1",
2425
"pinia": "^2.1.6",
2526
"swagger-ui": "^5.10.5",

frontend/src/stores/map.js

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { ref } from 'vue'
33
import { ENDPOINTS } from '@/constants'
44
import L from 'leaflet'
55
import 'leaflet-iconmaterial/dist/leaflet.icon-material'
6+
import 'leaflet.markercluster';
67

78
export const useMapStore = defineStore('map', () => {
89
const leaflet = ref(null)
@@ -11,6 +12,33 @@ export const useMapStore = defineStore('map', () => {
1112
const perceptualModelsGeojson = ref([])
1213
const mapLoaded = ref(false)
1314
let currentFilteredData = ref([])
15+
const markerClusterGroup = L.markerClusterGroup({
16+
iconCreateFunction: (cluster) => {
17+
const childCount = cluster.getChildCount();
18+
let color = 'blue';
19+
if (childCount > 10) {
20+
color = 'red';
21+
}
22+
23+
return L.divIcon({
24+
html: `
25+
<div style="
26+
background-color: ${color};
27+
border-radius: 50%;
28+
color: white;
29+
text-align: center;
30+
line-height: 40px;
31+
width: 40px;
32+
height: 40px;
33+
box-shadow: 0 4px 10px ${color};
34+
">
35+
${childCount}
36+
</div>`,
37+
className: 'custom-cluster-icon',
38+
iconSize: [40, 40],
39+
});
40+
},
41+
});
1442

1543
function onEachFeature(feature, layer) {
1644
let content = `<h3>Perceptual model of <strong>${feature.properties.location.long_name}</strong></h3>`
@@ -118,7 +146,8 @@ export const useMapStore = defineStore('map', () => {
118146
},
119147
pointToLayer: pointToLayer
120148
})
121-
layerGroup.value.addLayer(modelFeatures.value)
149+
markerClusterGroup.addLayer(modelFeatures.value); // Add features to the cluster group
150+
layerGroup.value.addLayer(markerClusterGroup);
122151
}
123152

124153
function filterFeatures(filterFunction) {
@@ -138,7 +167,8 @@ export const useMapStore = defineStore('map', () => {
138167
})
139168

140169
// add filtered features
141-
layerGroup.value.addLayer(modelFeatures.value)
170+
markerClusterGroup.addLayer(modelFeatures.value);
171+
layerGroup.value.addLayer(markerClusterGroup);
142172
}
143173

144174
function resetFilter() {
@@ -148,7 +178,8 @@ export const useMapStore = defineStore('map', () => {
148178
onEachFeature(feature, layer)
149179
}
150180
})
151-
layerGroup.value.addLayer(modelFeatures.value)
181+
markerClusterGroup.addLayer(modelFeatures.value);
182+
layerGroup.value.addLayer(markerClusterGroup);
152183
}
153184

154185
return {

0 commit comments

Comments
 (0)