Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add marker cluster to map #40

Merged
merged 2 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"leaflet": "^1.9.4",
"leaflet-easybutton": "^2.4.0",
"leaflet-iconmaterial": "^1.1.0",
"leaflet.markercluster": "^1.5.3",
"pinia": "^2.1.6",
"swagger-ui": "^5.10.5",
"vee-validate": "^4.13.2",
Expand Down
38 changes: 35 additions & 3 deletions frontend/src/stores/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,42 @@ import { ref } from 'vue'
import { ENDPOINTS } from '@/constants'
import L from 'leaflet'
import 'leaflet-iconmaterial/dist/leaflet.icon-material'
import 'leaflet.markercluster';

export const useMapStore = defineStore('map', () => {
const leaflet = ref(null)
const layerGroup = ref(null)
const modelFeatures = ref({})
const perceptualModelsGeojson = ref([])
const mapLoaded = ref(false)
const markerClusterGroup = L.markerClusterGroup({
iconCreateFunction: (cluster) => {
const childCount = cluster.getChildCount();

let color = 'blue';
if (childCount > 10) {
color = 'red';
}

return L.divIcon({
html: `
<div style="
background-color: ${color};
border-radius: 50%;
color: white;
text-align: center;
line-height: 40px;
width: 40px;
height: 40px;
box-shadow: 0 4px 10px ${color};
">
${childCount}
</div>`,
className: 'custom-cluster-icon',
iconSize: [40, 40]
});
},
});

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

function filterFeatures(filterFunction) {
Expand All @@ -135,7 +165,8 @@ export const useMapStore = defineStore('map', () => {
pointToLayer: pointToLayer
})
// add filtered features
layerGroup.value.addLayer(modelFeatures.value)
markerClusterGroup.addLayer(modelFeatures.value);
layerGroup.value.addLayer(markerClusterGroup);
}

function resetFilter() {
Expand All @@ -145,7 +176,8 @@ export const useMapStore = defineStore('map', () => {
onEachFeature(feature, layer)
}
})
layerGroup.value.addLayer(modelFeatures.value)
markerClusterGroup.addLayer(modelFeatures.value);
layerGroup.value.addLayer(markerClusterGroup);
}

return {
Expand Down
Loading