dawarich/app/javascript/maps/layers.js
2025-04-27 17:13:19 +02:00

64 lines
1.8 KiB
JavaScript

// Import the maps configuration
// In non-self-hosted mode, we need to mount external maps_config.js to the container
import { mapsConfig as vectorMapsConfig } from './vector_maps_config';
import { mapsConfig as rasterMapsConfig } from './raster_maps_config';
export function createMapLayer(map, selectedLayerName, layerKey, selfHosted) {
const config = selfHosted === "true" ? rasterMapsConfig[layerKey] : vectorMapsConfig[layerKey];
if (!config) {
console.warn(`No configuration found for layer: ${layerKey}`);
return null;
}
let layer;
console.log("isSelfhosted: ", selfHosted)
if (selfHosted === "true") {
layer = L.tileLayer(config.url, {
maxZoom: config.maxZoom,
attribution: config.attribution,
crossOrigin: true,
// Add any other config properties that might be needed
});
} else {
layer = protomapsL.leafletLayer(
{
url: config.url,
flavor: config.flavor,
crossOrigin: true,
}
)
}
if (selectedLayerName === layerKey) {
return layer.addTo(map);
} else {
return layer;
}
}
// Helper function to create all map layers
export function createAllMapLayers(map, selectedLayerName, selfHosted) {
const layers = {};
const mapsConfig = selfHosted === "true" ? rasterMapsConfig : vectorMapsConfig;
Object.keys(mapsConfig).forEach(layerKey => {
layers[layerKey] = createMapLayer(map, selectedLayerName, layerKey, selfHosted);
});
return layers;
}
export function osmMapLayer(map, selectedLayerName) {
let layerName = 'OpenStreetMap';
let layer = L.tileLayer("https://tile.openstreetmap.org/{z}/{x}/{y}.png", {
maxZoom: 19,
attribution: "&copy; <a href='http://www.openstreetmap.org/copyright'>OpenStreetMap</a>",
});
if (selectedLayerName === layerName) {
return layer.addTo(map);
} else {
return layer;
}
}