400 lines
17 KiB
HTML
400 lines
17 KiB
HTML
{% extends 'default.html' %}
|
||
{% load static %}
|
||
{% load mysettings %}
|
||
{% block header_link %}
|
||
<!-- Select2 -->
|
||
<link rel="stylesheet"
|
||
href="{% static 'lte/plugins/select2/css/select2.min.css' %}?ver={% settings_value 'APP_VER' %}">
|
||
<link rel="stylesheet"
|
||
href="{% static 'lte/plugins/select2-bootstrap4-theme/select2-bootstrap4.min.css' %}?ver={% settings_value 'APP_VER' %}">
|
||
<link rel="stylesheet" href="{% static 'js/ol/theme/default/ol.css' %}">
|
||
|
||
{% endblock header_link %}
|
||
|
||
{% block content %}
|
||
<section class="content-header">
|
||
<div class="container-fluid">
|
||
<div class="row mb-2">
|
||
<div class="col-sm-6">
|
||
<h1 class="mb-2">Редактирование сотрудника</h1>
|
||
</div>
|
||
<div class="col-sm-6">
|
||
<ol class="breadcrumb float-sm-right">
|
||
<li class="breadcrumb-item"><a href="{% url 'mainindex' %}">Главная</a>
|
||
</li>
|
||
<li class="breadcrumb-item active">Редактирование сотрудника</li>
|
||
</ol>
|
||
</div>
|
||
</div>
|
||
</div><!-- /.container-fluid -->
|
||
</section>
|
||
|
||
<section class="content text-sm p-1">
|
||
<div class="card" style="height: 90%;">
|
||
|
||
<!-- /.card-header -->
|
||
<div class="card-body">
|
||
<div class="row h-100">
|
||
<div class="col-md-4 col-sm-12">
|
||
<form class="card" method="post">
|
||
{% csrf_token %}
|
||
<div class="card-header">
|
||
<h3 class="card-title">Общие данные</h3>
|
||
</div>
|
||
<!-- form start -->
|
||
<div class="card-body">
|
||
<div class="form-group">
|
||
<label for="name">Наименование</label>
|
||
{{ form.name }}
|
||
</div>
|
||
<div class="form-group">
|
||
<label for="uid">Идентификатор</label>
|
||
<input type="text" id="uid" name="uid" readonly value="{{ worker.uid }}"
|
||
class="form-control readonly">
|
||
</div>
|
||
</div>
|
||
<div class="card-footer">
|
||
<button type="submit" class="btn btn-primary">Сохранить</button>
|
||
</div>
|
||
</form>
|
||
</div>
|
||
<div class="col-md-8 col-sm-12">
|
||
<div class="card h-100">
|
||
<div class="card-header">
|
||
<h3 class="card-title">Местонахождение</h3>
|
||
</div>
|
||
<!-- form start -->
|
||
<div class="card-body p-0">
|
||
<div id="map"></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- /.card-body -->
|
||
</div>
|
||
</section>
|
||
{% endblock %}
|
||
|
||
{% block script_footer %}
|
||
<script src="{% static 'js/ol/lib/OpenLayers.js' %}?ver={% settings_value 'APP_VER' %}"></script>
|
||
<script>
|
||
let map = {}
|
||
let VlocationLayer = {}
|
||
let VlocationSource = {}
|
||
let popup = {}
|
||
let m_markers = {}
|
||
let VlocationMarkers = {}
|
||
|
||
function createWorkersTableRow(worker) {
|
||
const tr = document.createElement('tr')
|
||
|
||
const tdWorkerID = document.createElement('td')
|
||
tdWorkerID.innerHTML = `<a href="workers/edit/${worker.id}/">${worker.name}</a>`
|
||
|
||
const tdWorkerCoord = document.createElement('td')
|
||
tdWorkerCoord.textContent = `${worker.lat}, ${worker.lon}`
|
||
|
||
tr.appendChild(tdWorkerID)
|
||
tr.appendChild(tdWorkerCoord)
|
||
return tr
|
||
}
|
||
|
||
function VlocationStyleFunction(feature, resolution) {
|
||
return new ol.style.Style({
|
||
image: new ol.style.Icon({
|
||
src: '/static/img/worker.png',
|
||
width: 25,
|
||
height: 30
|
||
})
|
||
})
|
||
}
|
||
|
||
function moveMarker(marker, newCoordinates) {
|
||
const duration = 2000; // Длительность анимации в миллисекундах
|
||
const startCoordinates = marker.getGeometry().getCoordinates();
|
||
|
||
// Начало анимации
|
||
const startTime = performance.now();
|
||
|
||
function animate(currentTime) {
|
||
// Вычисление прошедшего времени
|
||
const elapsed = currentTime - startTime;
|
||
|
||
// Вычисление доли анимации (от 0 до 1)
|
||
const fraction = Math.min(elapsed / duration, 1);
|
||
|
||
// Интерполяция координат
|
||
const interpolatedCoordinates = [
|
||
startCoordinates[0] + fraction * (newCoordinates[0] - startCoordinates[0]),
|
||
startCoordinates[1] + fraction * (newCoordinates[1] - startCoordinates[1]),
|
||
];
|
||
|
||
// Устанавливаем новые координаты для маркера
|
||
marker.getGeometry().setCoordinates(interpolatedCoordinates);
|
||
|
||
// Продолжаем анимацию, если она еще не завершена
|
||
if (fraction < 1) {
|
||
requestAnimationFrame(animate);
|
||
}
|
||
}
|
||
|
||
// Запуск анимации
|
||
requestAnimationFrame(animate);
|
||
}
|
||
|
||
function initializeMap() {
|
||
// Источник данных для маркеров
|
||
VlocationSource = new ol.source.Vector();
|
||
|
||
// Слой для отображения маркеров
|
||
VlocationLayer = new ol.layer.Vector({
|
||
source: VlocationSource,
|
||
style: VlocationStyleFunction
|
||
});
|
||
|
||
// Создание карты
|
||
map = new ol.Map({
|
||
target: 'map', // ID элемента, куда встроится карта
|
||
controls: [
|
||
{#new ol.control.Zoom(),#}
|
||
new ol.control.FullScreen(),
|
||
new ol.control.ScaleLine(),
|
||
new ol.control.ZoomSlider()
|
||
],
|
||
layers: [
|
||
new ol.layer.Tile({
|
||
source: new ol.source.OSM(), // Open street Map
|
||
}),
|
||
VlocationLayer,
|
||
],
|
||
view: new ol.View({
|
||
center: ol.proj.fromLonLat(['{{ worker.longitude }}'.replace(",", "."), parseFloat('{{ worker.latitude }}'.replace(",", "."))]), // Центр карты
|
||
zoom: 13,
|
||
}),
|
||
});
|
||
const element = document.getElementById('popup')
|
||
// Всплывающее окно
|
||
popup = new ol.Overlay({
|
||
element: element,
|
||
positioning: 'bottom-center',
|
||
{#autoPan: true,#}
|
||
});
|
||
map.addOverlay(popup);
|
||
|
||
let popover;
|
||
|
||
function disposePopover() {
|
||
if (popover) {
|
||
popover.dispose();
|
||
popover = undefined;
|
||
}
|
||
}
|
||
|
||
// Обработка кликов по маркерам
|
||
map.on('click', function (evt) {
|
||
// Получение feature по клику
|
||
const feature = map.forEachFeatureAtPixel(evt.pixel, function (feature) {
|
||
return feature;
|
||
});
|
||
|
||
// Удаление существующего popover
|
||
disposePopover();
|
||
|
||
if (!feature) {
|
||
return; // Если feature не найден, ничего не делаем
|
||
}
|
||
|
||
// Получение координат клика
|
||
const coordinates = evt.coordinate;
|
||
|
||
// Получение контейнера элемента popup
|
||
const element = document.getElementById('popup'); // Убедитесь, что этот элемент существует в DOM
|
||
|
||
// Установка позиции popup на карте
|
||
popup.setPosition(coordinates);
|
||
|
||
function createPopover(feature) {
|
||
const res = document.createElement('div')
|
||
const properties = feature.getProperties()
|
||
for (let property in properties) {
|
||
switch (property) {
|
||
case 'name': {
|
||
let div = document.createElement('div')
|
||
let span = document.createElement('span')
|
||
let strong = document.createElement('strong')
|
||
|
||
strong.textContent = "Имя: "
|
||
div.appendChild(strong)
|
||
|
||
span.textContent = feature.get('name')
|
||
div.appendChild(span)
|
||
|
||
res.appendChild(div)
|
||
break
|
||
}
|
||
case 'lat': {
|
||
let div = document.createElement('div')
|
||
let span = document.createElement('span')
|
||
let strong = document.createElement('strong')
|
||
|
||
strong.textContent = "Координаты: "
|
||
div.appendChild(strong)
|
||
|
||
span.textContent = feature.get('lat') + ' ' + feature.get('lon')
|
||
div.appendChild(span)
|
||
|
||
res.appendChild(div)
|
||
break
|
||
}
|
||
case 'time': {
|
||
let div = document.createElement('div')
|
||
let span = document.createElement('span')
|
||
let strong = document.createElement('strong')
|
||
|
||
strong.textContent = "Время координаты: "
|
||
div.appendChild(strong)
|
||
|
||
span.textContent = feature.get('time')
|
||
div.appendChild(span)
|
||
|
||
res.appendChild(div)
|
||
break
|
||
}
|
||
case 'statenum': {
|
||
let div = document.createElement('div')
|
||
let span = document.createElement('span')
|
||
let strong = document.createElement('strong')
|
||
|
||
strong.textContent = "Госномер: "
|
||
div.appendChild(strong)
|
||
|
||
span.textContent = feature.get('statenum')
|
||
div.appendChild(span)
|
||
|
||
res.appendChild(div)
|
||
break
|
||
}
|
||
case "depth": {
|
||
let div = document.createElement('div')
|
||
let span = document.createElement('span')
|
||
let strong = document.createElement('strong')
|
||
|
||
strong.textContent = "Глубина: "
|
||
div.appendChild(strong)
|
||
|
||
span.textContent = feature.get('depth')
|
||
div.appendChild(span)
|
||
|
||
res.appendChild(div)
|
||
break
|
||
}
|
||
case "magnitude": {
|
||
let div = document.createElement('div')
|
||
let span = document.createElement('span')
|
||
let strong = document.createElement('strong')
|
||
|
||
strong.textContent = "Магнитуда: "
|
||
div.appendChild(strong)
|
||
|
||
span.textContent = feature.get('magnitude') + " км."
|
||
div.appendChild(span)
|
||
|
||
res.appendChild(div)
|
||
break
|
||
}
|
||
case "region": {
|
||
let div = document.createElement('div')
|
||
let span = document.createElement('span')
|
||
let strong = document.createElement('strong')
|
||
|
||
strong.textContent = "Регион: "
|
||
div.appendChild(strong)
|
||
|
||
span.textContent = feature.get('region')
|
||
div.appendChild(span)
|
||
|
||
res.appendChild(div)
|
||
break
|
||
}
|
||
case "source": {
|
||
let div = document.createElement('div')
|
||
let span = document.createElement('span')
|
||
let strong = document.createElement('strong')
|
||
|
||
strong.textContent = "Источник: "
|
||
div.appendChild(strong)
|
||
|
||
span.textContent = feature.get('source')
|
||
div.appendChild(span)
|
||
|
||
res.appendChild(div)
|
||
break
|
||
}
|
||
}
|
||
}
|
||
return res
|
||
}
|
||
|
||
// Создание нового popover
|
||
popover = new bootstrap.Popover(element, {
|
||
placement: 'top', // Расположение popover
|
||
html: true, // Поддержка HTML-содержимого
|
||
content: createPopover(feature),
|
||
});
|
||
|
||
// Отображение popover
|
||
popover.show();
|
||
});
|
||
map.on('movestart', disposePopover);
|
||
|
||
// Загрузка маркеров
|
||
updateMarkers();
|
||
setInterval(updateMarkers, 5000)
|
||
}
|
||
|
||
function addVlocationMarker(worker) {
|
||
const point = new ol.geom.Point(ol.proj.fromLonLat([worker.lon, worker.lat]));
|
||
const feature = new ol.Feature(point);
|
||
feature.setProperties({
|
||
id: worker.id,
|
||
name: worker.name,
|
||
lat: worker.lat,
|
||
lon: worker.lon,
|
||
time: worker.time,
|
||
})
|
||
VlocationSource.addFeature(feature)
|
||
VlocationMarkers[worker.id] = feature
|
||
}
|
||
|
||
function updateMarkers() {
|
||
fetch('{% url 'vlocation_update_object_with_id' worker.id %}', {
|
||
method: 'POST',
|
||
headers: {'X-CSRFToken': csrftoken},
|
||
mode: 'same-origin',
|
||
})
|
||
.then(response => response.json())
|
||
.then(data => {
|
||
data.forEach(worker => {
|
||
if (VlocationMarkers[worker.id]) {
|
||
const marker = VlocationMarkers[worker.id];
|
||
const currentCoordinates = marker.getGeometry().getCoordinates();
|
||
const newCoordinates = ol.proj.fromLonLat([worker.lon, worker.lat]);
|
||
|
||
// Проверяем, изменились ли координаты
|
||
if (currentCoordinates[0] !== newCoordinates[0] || currentCoordinates[1] !== newCoordinates[1]) {
|
||
moveMarker(marker, newCoordinates); // Плавное перемещение
|
||
}
|
||
} else {
|
||
// Создаем новый маркер, если его ещё нет
|
||
addVlocationMarker(worker);
|
||
}
|
||
});
|
||
});
|
||
}
|
||
|
||
|
||
window.onload = initializeMap;
|
||
</script>
|
||
{% endblock %} |