From b28f9c2783b8f7ccf36d7151672f7c5cc22b24e8 Mon Sep 17 00:00:00 2001 From: bernardhanna Date: Mon, 9 Jun 2025 10:18:38 +0100 Subject: [PATCH] Fix: map markers not showing on initial load (global cluster group) --- public/js/customMap.js | 154 ++++++++++++++++++++++------------------- 1 file changed, 83 insertions(+), 71 deletions(-) diff --git a/public/js/customMap.js b/public/js/customMap.js index cf628b386..55050c2e8 100644 --- a/public/js/customMap.js +++ b/public/js/customMap.js @@ -1,5 +1,5 @@ L.custom = { - init: function (obj, params) { // this method is run as soon as the core loading process is loaded + init: function (obj, params) { window.map = L.map(obj, { center: [48, 4], @@ -11,96 +11,107 @@ L.custom = { map.menu.remove("print"); - var markersCountryLayers = []; - - var success = function (data) { - - var markerOnClick = function (e) { - var id = e.target.options.id; - $.ajax({ - dataType: "json", - url: "api/event/detail?id=" + id, - success: function (res) { - var event = res.data; - - var content = '

' + event.title + '

' + - '' + - '

' + event.description + '

'; + // Define one global marker cluster group + var globalMarkersGroup = L.markerClusterGroup({ + showCoverageOnHover: false, + maxClusterRadius: 120, + chunkedLoading: true, + iconCreateFunction: function (cluster) { + var total = cluster.getAllChildMarkers().length; + var iconSize; + var className = "mycluster "; + if (total <= 10) { + iconSize = L.point(30, 30); + className += "size1"; + } else if (total <= 100) { + iconSize = L.point(35, 35); + className += "size2"; + } else if (total <= 1000) { + iconSize = L.point(40, 40); + className += "size3"; + } else if (total <= 10000) { + iconSize = L.point(45, 45); + className += "size4"; + } else { + iconSize = L.point(50, 50); + className += "size5"; + } + return L.divIcon({ html: '
' + total + '
', className: className, iconSize: iconSize }); + } + }); - var popup = L.popup({ maxWidth: 600 }) - .setContent(content) + map.addLayer(globalMarkersGroup); // Add once - e.target.bindPopup(popup).openPopup(); - } - }); - } + // Marker click + var markerOnClick = function (e) { + var id = e.target.options.id; + $.ajax({ + dataType: "json", + url: "api/event/detail?id=" + id, + success: function (res) { + var event = res.data; + var content = '

' + event.title + '

' + + '' + + '

' + event.description + '

'; - // Clear previous layers if needed - if (markersCountryLayers.length > 0) { - $.each(markersCountryLayers, function (key, layer) { - layer.clearLayers(); - map.removeLayer(layer); - }); - markersCountryLayers = []; - } + var popup = L.popup({ maxWidth: 600 }) + .setContent(content); - // Create ONE global cluster group - var allMarkersClusterGroup = L.markerClusterGroup({ - showCoverageOnHover: false, - maxClusterRadius: 120, - chunkedLoading: true, - iconCreateFunction: function (cluster) { - var total = cluster.getAllChildMarkers().length; - var iconSize; - var className = "mycluster "; - if (total <= 10) { - iconSize = L.point(30, 30); - className += "size1"; - } else if (total <= 100) { - iconSize = L.point(35, 35); - className += "size2"; - } else if (total <= 1000) { - iconSize = L.point(40, 40); - className += "size3"; - } else if (total <= 10000) { - iconSize = L.point(45, 45); - className += "size4"; - } else { - iconSize = L.point(50, 50); - className += "size5"; - } - return L.divIcon({ html: '
' + total + '
', className: className, iconSize: iconSize }); + e.target.bindPopup(popup).openPopup(); } }); + }; + + // Success callback + var success = function (data) { + + // Clear existing markers first + globalMarkersGroup.clearLayers(); - // Add ALL markers to ONE cluster group - $.each(data, function (key, country) { - $.each(country, function (key, val) { + // Loop over countries and events + $.each(data, function (countryCode, countryEvents) { + $.each(countryEvents, function (key, val) { var coordinates = val.geoposition.split(','); - var marker = L.marker(L.latLng(coordinates[0], coordinates[1]), { id: val.id }); + var marker = L.marker(L.latLng(coordinates[0], coordinates[1]), { + id: val.id, + country: countryCode // store country on marker for filtering + }); marker.on('click', markerOnClick); - allMarkersClusterGroup.addLayer(marker); + globalMarkersGroup.addLayer(marker); }); }); - // Add the global cluster group to the map - map.addLayer(allMarkersClusterGroup); - - // Save reference - markersCountryLayers.push(allMarkersClusterGroup); - - // process next components + // Process next components $wt._queue("next"); - } + }; + + // Country filter + $('#id_country').on('change', function () { + var selectedCountry = this.value; + + // Loop through all markers and toggle visibility + globalMarkersGroup.eachLayer(function (marker) { + if (!selectedCountry || selectedCountry === '') { + marker.setOpacity(1); // Show all + } else if (marker.options.country === selectedCountry) { + marker.setOpacity(1); // Show matching + } else { + marker.setOpacity(0); // Hide others + } + }); + }); + // Year filter $('#id_year').on('change', function () { getEvents(this.value); }); + // Get URL param function param(name) { return (location.search.split(name + '=')[1] || '').split('&')[0]; } + // AJAX fetch events function getEvents(year) { $.ajax({ dataType: "json", @@ -109,7 +120,8 @@ L.custom = { }); } - var year = param('year') ? param('year') : 2019; + // Initial load + var year = param('year') ? param('year') : new Date().getFullYear(); getEvents(year); } -} +};