// public/js/mapPhotosLayer.js // =============================== // LAYER MAPPA FOTO // =============================== (function () { let visiblePhotoMarkers = new Map(); window.clearPhotoMapLayer = function (clearFactoryCache = false) { if (window.globalMarkers) { visiblePhotoMarkers = MapLayerManager.sync( window.globalMarkers, visiblePhotoMarkers, [], ); } else { visiblePhotoMarkers.clear(); } if (clearFactoryCache) { PhotoMarkerFactory.clear(); } console.log( "[mapPhotosLayer] marker Leaflet puliti", { visible: visiblePhotoMarkers.size, cache: PhotoMarkerFactory.size(), clusterLayers: window.globalMarkers ?.getLayers?.() ?.length ?? null, }, ); }; window.photoMapDebug = { cacheSize() { return PhotoMarkerFactory.size(); }, visibleSize() { return visiblePhotoMarkers.size; }, clearCache() { window.clearPhotoMapLayer?.( true, ); }, }; // =============================== // APRI FOTO // =============================== window.openPhotoMapItem = function (item) { const thumb = toAbsoluteUrl( item.thub1 || item.thub2 || item.path, item.name, "thumbs", item.cartella, ); const original = toAbsoluteUrl( item.path, item.name, "original", item.cartella, ); window.openModal(item); //window.openModal?.(original, thumb, item); }; // =============================== // RENDER FOTO // =============================== window.renderPhotoMapLayer = function () { if ( window.state?.mode !== "photos" ) { return; } if (!window.globalMap) { return; } if (!window.MapEngine?.isReady?.()) { console.warn("[mapPhotosLayer] MapEngine non pronto"); return; } const bounds = window.globalMap.getBounds(); if (!MapSpatialIndex.isBuilt()) { MapSpatialIndex.build(getLocalItems()); } const items = MapSpatialIndex.search(bounds); const markers = []; for (const entry of items) { const lat = entry.lat; const lng = entry.lng; const photo = entry.item; if (!Number.isFinite(lat) || !Number.isFinite(lng)) { continue; } const marker = PhotoMarkerFactory.get(photo); markers.push(marker); } visiblePhotoMarkers = MapLayerManager.sync( window.globalMarkers, visiblePhotoMarkers, markers, ); for (const marker of markers) { if ( marker && !window.globalMarkers ?.hasLayer?.(marker) ) { window.globalMarkers ?.addLayer?.(marker); } } console.log( "[mapPhotosLayer] marker sincronizzati", { visible: visiblePhotoMarkers.size, clusterLayers: window.globalMarkers ?.getLayers?.() ?.length ?? null, }, ); if (window.MapTerrain3D?.enabled?.()) { const features = window.getPhotoMapGeoJSON?.() || []; window.MapboxMarkers?.setPhotos(features); } console.log( "[mapPhotosLayer]", "visibili:", visiblePhotoMarkers.size, "cache:", PhotoMarkerFactory.size(), "indice:", items.length, ); }; // =============================== // GEOJSON PER MAPBOX // =============================== window.getPhotoMapGeoJSON = function () { if (!MapSpatialIndex.isBuilt()) { MapSpatialIndex.build(getLocalItems()); } const features = []; let items = []; /* if (window.MapEngine?.isMapbox?.()) { items = MapSpatialIndex.search({ getWest: () => -180, getEast: () => 180, getSouth: () => -90, getNorth: () => 90, }); } else { const bounds = window.MapEngine?.getBounds?.(); items = bounds ? MapSpatialIndex.search(bounds) : []; } */ const bounds = window.MapEngine?.getBounds?.(); items = bounds ? MapSpatialIndex.search(bounds) : []; console.log("[PhotoGeoJSON DEBUG bounds]", { west: bounds?.getWest?.(), east: bounds?.getEast?.(), south: bounds?.getSouth?.(), north: bounds?.getNorth?.(), }); console.log("[PhotoGeoJSON DEBUG items]", items.length); for (const entry of items) { const lat = entry.lat; const lng = entry.lng; const photo = entry.item; if (!Number.isFinite(lat) || !Number.isFinite(lng)) { continue; } features.push({ type: "Feature", geometry: { type: "Point", coordinates: [lng, lat], }, /* properties: { id: photo.id, type: "photo", name: photo.name || "", },*/ properties: { id: photo.id, type: "photo", name: photo.name || "", thumb: toAbsoluteUrl( photo.thub1 || photo.thub2 || photo.path, photo.name, "thumbs", photo.cartella, ), }, }); } return features; }; })();