Skip to content
Merged
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
154 changes: 83 additions & 71 deletions public/js/customMap.js
Original file line number Diff line number Diff line change
@@ -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],
Expand All @@ -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 = '<div><h4><a href="' + event.path + '" class="map-marker">' + event.title + '</a></h4><div style="display:flex;align-items: center;">' +
'<img src="' + event.picture + '" class="img-polaroid marker-buble-img" style="width:100px;height:100px;">' +
'<p style="overflow:hidden;">' + event.description + '</p>';
// 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: '<div>' + total + '</div>', 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 = '<div><h4><a href="' + event.path + '" class="map-marker">' + event.title + '</a></h4><div style="display:flex;align-items: center;">' +
'<img src="' + event.picture + '" class="img-polaroid marker-buble-img" style="width:100px;height:100px;">' +
'<p style="overflow:hidden;">' + event.description + '</p>';

// 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: '<div>' + total + '</div>', 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",
Expand All @@ -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);
}
}
};