Ограничение перемещения

This commit is contained in:
zakhy 2025-05-14 14:03:46 +07:00
parent 5e91f6472c
commit 82af1576fc

View File

@ -270,59 +270,87 @@
});
</script>
<script>
document.addEventListener("DOMContentLoaded", async () => {
const extent = ol.proj.transformExtent(
[
parseFloat('{{ form.instance.min_lon|safe }}'),
parseFloat('{{ form.instance.min_lat|safe }}'),
parseFloat('{{ form.instance.max_lon|safe }}'),
parseFloat('{{ form.instance.max_lat|safe }}')
],
'EPSG:4326', // Исходная проекция
'EPSG:3857' // Целевая проекция
);
const mapCore = new MapCore('map', {
center: ['{{longitude}}', '{{latitude}}'],
zoom: 12
});
await mapCore.initialize();
const scanLayer = new ol.layer.Tile({
source: new ol.source.XYZ({
url: `{{ form.instance.url }}`,
document.addEventListener("DOMContentLoaded", async () => {
// Получаем и трансформируем extent
const extent = ol.proj.transformExtent(
[
parseFloat('{{ form.instance.min_lon|safe }}'),
parseFloat('{{ form.instance.min_lat|safe }}'),
parseFloat('{{ form.instance.max_lon|safe }}'),
parseFloat('{{ form.instance.max_lat|safe }}')
],
'EPSG:4326', // Исходная проекция
'EPSG:3857' // Целевая проекция
);
// Создаем padded extent с отступом 20 единиц
const padding = 20; // Можно настроить по вашему усмотрению
const paddedExtent = ol.extent.buffer(extent, padding);
// Инициализируем карту с ограничениями
const mapCore = new MapCore('map', {
center: ['{{longitude}}', '{{latitude}}'],
zoom: 12,
view: new ol.View({
extent: paddedExtent, // Устанавливаем ограниченный extent
constrainOnlyCenter: false, // Ограничиваем всю карту
multiWorld: false // Запрещаем бесконечный скролл
})
});
await mapCore.initialize();
// Добавляем скан-слой с оригинальным extent
const scanLayer = new ol.layer.Tile({
source: new ol.source.XYZ({
url: `{{ form.instance.url }}`,
}),
extent: extent // Оригинальный extent без padding
});
scanLayer.set('title', 'Ортофотоплан')
scanLayer.set('name', 'orto')
mapCore.addLayer(scanLayer)
// Подгоняем карту к оригинальному extent с отступами
mapCore.map.getView().fit(extent, {
padding: [20, 20, 20, 20],
duration: 1000,
maxZoom: 15
});
// Создаем полигон для визуализации extent
const extentPolygon = ol.geom.Polygon.fromExtent(extent);
const vectorLayer = new ol.layer.Vector({
source: new ol.source.Vector({
features: [
new ol.Feature({
geometry: extentPolygon,
}),
extent: extent
});
scanLayer.set('title', 'Ортофотоплан')
scanLayer.set('name', 'orto')
mapCore.addLayer(scanLayer)
],
}),
style: new ol.style.Style({
stroke: new ol.style.Stroke({
color: 'red',
width: 2,
}),
}),
});
mapCore.addLayer(vectorLayer);
// Дополнительная проверка границ (опционально)
mapCore.map.getView().on('change:center', () => {
const view = mapCore.map.getView();
const currentCenter = view.getCenter();
const currentExtent = view.calculateExtent(mapCore.map.getSize());
mapCore.map.getView().fit(extent, {
padding: [20, 20, 20, 20],
duration: 1000,
maxZoom: 15
});
const extentPolygon = ol.geom.Polygon.fromExtent(extent);
const vectorLayer = new ol.layer.Vector({
source: new ol.source.Vector({
features: [
new ol.Feature({
geometry: extentPolygon,
}),
],
}),
style: new ol.style.Style({
stroke: new ol.style.Stroke({
color: 'red',
width: 2,
}),
}),
});
mapCore.addLayer(vectorLayer)
});
if (!ol.extent.containsExtent(paddedExtent, currentExtent)) {
view.setCenter([
Math.max(paddedExtent[0], Math.min(paddedExtent[2], currentCenter[0])),
Math.max(paddedExtent[1], Math.min(paddedExtent[3], currentCenter[1]))
]);
}
});
});
</script>