разделение по группам слоев

This commit is contained in:
zakhy 2025-02-19 08:43:00 +07:00
parent 58458748bc
commit fd0ddff4a1

View File

@ -136,49 +136,6 @@
const slider = document.getElementById('time-slider');
const sliderDate = document.getElementById('slider-date');
function fetchLayers() {
fetch('layers/')
.then(response => response.json())
.then(data => {
data["static_layers"].forEach(layer => {
addLayerToMap(layer, 'static');
})
data["dynamic_layers"].forEach(layer => {
addLayerToMap(layer, 'dynamic');
})
})
.catch(error => console.error('Ошибка при загрузке слоев:', error));
}
function addLayerToMap(layer, type="static") {
let newLayer;
if (layer.layer_type === 'Tile') {
newLayer = new ol.layer.Tile({
source: new ol.source.XYZ({
url: layer.url,
}),
visible: layer.is_visible,
});
} else if (layer.layer_type === 'Vector') {
newLayer = new ol.layer.Vector({
source: new ol.source.Vector({
url: layer.url,
format: new ol.format.GeoJSON(),
}),
visible: layer.is_visible,
style: polygonBrownStyleFunction,
});
}
if (newLayer) {
newLayer.set('name', layer.name);
map.addLayer(newLayer);
}
}
if (!slider) {
console.error('Слайдер не найден!');
@ -463,6 +420,71 @@
GTOZonePolygonsLayer.set('title', 'Зоны')
const staticLayers = [
GTOZonePolygonsLayer,
mineBorderLayer,
]
const dynamicLayers = [
SPLayer,
VlocationLayer,
GTOLayer,
]
function fetchLayers() {
fetch('layers/')
.then(response => response.json())
.then(data => {
data["static_layers"].forEach(layer => {
addLayerToMap(layer, 'static');
})
data["dynamic_layers"].forEach(layer => {
addLayerToMap(layer, 'dynamic');
})
})
.catch(error => console.error('Ошибка при загрузке слоев:', error));
}
function addLayerToMap(layer, type = "static") {
let newLayer;
if (layer.layer_type === 'Tile') {
newLayer = new ol.layer.Tile({
source: new ol.source.XYZ({
url: layer.url,
}),
visible: layer.is_visible,
});
} else if (layer.layer_type === 'Vector') {
newLayer = new ol.layer.Vector({
source: new ol.source.Vector({
url: layer.url,
format: new ol.format.GeoJSON(),
}),
visible: layer.is_visible,
style: polygonBrownStyleFunction,
});
}
if (newLayer) {
newLayer.set('title', layer.name);
if (type === 'static') {
staticLayers.push(newLayer)
} else if (type === 'dynamic') {
dynamicLayers.push(newLayer)
}
}
}
const scanLayerExists = Boolean(parseInt('{{ scan_layer_enable }}'))
if (scanLayerExists) {
staticLayers.splice(0, 0, scanLayer)
}
fetchLayers()
const layers = [
new ol.layer.Group({
title: 'Базовые слои',
@ -472,30 +494,14 @@
new ol.layer.Group({
title: 'Статические слои',
openInLayerSwitcher: true,
layers: [
GTOZonePolygonsLayer,
mineBorderLayer,
],
layers: staticLayers,
}),
new ol.layer.Group({
title: 'Динамические слои',
openInLayerSwitcher: true,
layers: [
SPLayer,
VlocationLayer,
GTOLayer,
],
layers: dynamicLayers,
}),
]
const scanLayerExists = Boolean(parseInt('{{ scan_layer_enable }}'))
if (scanLayerExists) {
layers.splice(1, 0, scanLayer)
}
// Создание карты
map = new ol.Map({
target: 'map', // ID элемента, куда встроится карта
@ -879,7 +885,6 @@
})
window.onload = function () {
initializeMap();
fetchLayers();
}
</script>