Skip to content

Commit 8328d32

Browse files
authored
Merge pull request #40 from CUAHSI/add-marker-cluster-to-map
Add marker cluster to map
2 parents c5a9b5f + b7917a1 commit 8328d32

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

frontend/package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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
"pinia": "^2.1.6",
2425
"swagger-ui": "^5.10.5",
2526
"vee-validate": "^4.13.2",

frontend/src/stores/map.js

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,42 @@ 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)
910
const layerGroup = ref(null)
1011
const modelFeatures = ref({})
1112
const perceptualModelsGeojson = ref([])
1213
const mapLoaded = ref(false)
14+
const markerClusterGroup = L.markerClusterGroup({
15+
iconCreateFunction: (cluster) => {
16+
const childCount = cluster.getChildCount();
17+
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+
});
1342

1443
function onEachFeature(feature, layer) {
1544
let content = `<h3>Perceptual model of <strong>${feature.properties.location.long_name}</strong></h3>`
@@ -117,7 +146,8 @@ export const useMapStore = defineStore('map', () => {
117146
},
118147
pointToLayer: pointToLayer
119148
})
120-
layerGroup.value.addLayer(modelFeatures.value)
149+
markerClusterGroup.addLayer(modelFeatures.value); // Add features to the cluster group
150+
layerGroup.value.addLayer(markerClusterGroup);
121151
}
122152

123153
function filterFeatures(filterFunction) {
@@ -135,7 +165,8 @@ export const useMapStore = defineStore('map', () => {
135165
pointToLayer: pointToLayer
136166
})
137167
// add filtered features
138-
layerGroup.value.addLayer(modelFeatures.value)
168+
markerClusterGroup.addLayer(modelFeatures.value);
169+
layerGroup.value.addLayer(markerClusterGroup);
139170
}
140171

141172
function resetFilter() {
@@ -145,7 +176,8 @@ export const useMapStore = defineStore('map', () => {
145176
onEachFeature(feature, layer)
146177
}
147178
})
148-
layerGroup.value.addLayer(modelFeatures.value)
179+
markerClusterGroup.addLayer(modelFeatures.value);
180+
layerGroup.value.addLayer(markerClusterGroup);
149181
}
150182

151183
return {

0 commit comments

Comments
 (0)