diff --git a/public/js/customMap.js b/public/js/customMap.js index 9d5af931a..f1486d385 100644 --- a/public/js/customMap.js +++ b/public/js/customMap.js @@ -1,5 +1,6 @@ L.custom = { init: function (obj, params) { + window.map = L.map(obj, { center: [48, 4], zoom: 4, @@ -10,83 +11,95 @@ L.custom = { map.menu.remove("print"); - var masterCluster = 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 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 markersCountryLayers = []; - var content = '

' + event.title + '

' + - '' + - '

' + event.description + '

'; + var success = function (data) { - var popup = L.popup({ maxWidth: 600 }).setContent(content); + console.log('đŸ”Ĩ Full API response:', data); - e.target.bindPopup(popup).openPopup(); - } - }); - }; + // SAFELY get the correct countries object + var countryData = Array.isArray(data) && data.length > 1 ? data[1] : data; - var success = function (data) { - console.log('✅ API returned countries:', Object.keys(data)); + console.log('✅ Using countryData:', countryData); - // Clear old cluster - masterCluster.clearLayers(); + // Clear old layers + if (markersCountryLayers.length > 0) { + $.each(markersCountryLayers, function (key, countryLayer) { + countryLayer.clearLayers(); + map.removeLayer(countryLayer); + }); + markersCountryLayers = []; + } + // NEW: Build master cluster var allMarkers = []; - $.each(data, function (key, country) { - $.each(country, function (key, val) { - if (val.geoposition) { - var coordinates = val.geoposition.split(','); + $.each(countryData, function (countryIso, events) { + $.each(events, function (idx, event) { + if (event.geoposition) { + var coordinates = event.geoposition.split(','); var lat = parseFloat(coordinates[0]); var lng = parseFloat(coordinates[1]); if (!isNaN(lat) && !isNaN(lng)) { - var marker = L.marker([lat, lng], { id: val.id }); - marker.on('click', markerOnClick); + var marker = L.marker(L.latLng(lat, lng), { id: event.id }); + marker.on('click', function (e) { + var id = e.target.options.id; + $.ajax({ + dataType: "json", + url: "api/event/detail?id=" + id, + success: function (res) { + var ev = res.data; + var content = '

' + ev.title + '

' + + '
' + + '' + + '

' + ev.description + '

'; + + var popup = L.popup({ maxWidth: 600 }).setContent(content); + e.target.bindPopup(popup).openPopup(); + } + }); + }); allMarkers.push(marker); } } }); }); - console.log('✅ Adding', allMarkers.length, 'markers to master cluster'); - masterCluster.addLayers(allMarkers); + var masterCluster = 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 }); + } + }); + masterCluster.addLayers(allMarkers); + markersCountryLayers.push(masterCluster); map.addLayer(masterCluster); - // process next components + console.log('✅ Markers added:', allMarkers.length); + $wt._queue("next"); }; @@ -99,7 +112,6 @@ L.custom = { } function getEvents(year) { - console.log('â„šī¸ Loading events for year', year); $.ajax({ dataType: "json", url: "api/event/list?year=" + year,