{# Partial to add MarkerCluster support and user geolocation to a Leaflet map.
Expects: mapVar (JS variable name of the map, default 'mymap')
markersVar (JS variable name of the MarkerClusterGroup, default 'markers')
#}
{% set mapVar = mapVar|default('mymap') %}
{% set markersVar = markersVar|default('markers') %}
{# Add all markers from the cluster group to the map #}
{{ mapVar }}.addLayer({{ markersVar }});
{# Geolocation: only if ALLOW_GEOLOCATION is enabled #}
{% if KOH_ALLOW_GEOLOCATION|default('false') == 'true' %}
(function() {
var geolocMap = {{ mapVar }};
var geolocConsent = localStorage.getItem('kohinos_geoloc_consent');
function addUserMarker(lat, lng) {
var userIcon = L.divIcon({
html: '',
iconSize: [24, 24],
iconAnchor: [12, 24],
className: 'kohinos-user-geoloc-icon'
});
var userMarker = L.marker([lat, lng], {icon: userIcon, zIndexOffset: 1000}).addTo(geolocMap);
userMarker.bindPopup('Vous ĂȘtes ici');
}
function requestGeolocation() {
if (!navigator.geolocation) return;
navigator.geolocation.getCurrentPosition(
function(position) {
localStorage.setItem('kohinos_geoloc_consent', 'granted');
addUserMarker(position.coords.latitude, position.coords.longitude);
},
function(error) {
// User denied or error: remove stored consent so we ask again next time
localStorage.removeItem('kohinos_geoloc_consent');
},
{ enableHighAccuracy: false, timeout: 10000, maximumAge: 300000 }
);
}
if (geolocConsent === 'granted') {
// Previously accepted: request silently (browser may remember permission)
requestGeolocation();
} else {
// Not yet accepted: request (browser will show its own permission dialog)
requestGeolocation();
}
})();
{% endif %}