391 lines
16 KiB
HTML
391 lines
16 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">
|
||
<div class="container-fluid ">
|
||
<div class="row">
|
||
<div class="col-md-3">
|
||
<div class="card">
|
||
<div class="card-body table-responsive p-0" style="height: 85vh; padding-left: 0;">
|
||
<table class="table table-bordered table-hover table-head-fixed text-nowrap text-sm">
|
||
<thead>
|
||
<tr role="row">
|
||
<th>Сотрудники <span id="workersCount">( 0 )</span></th>
|
||
<th></th>
|
||
</tr>
|
||
</thead>
|
||
<tbody id="workersTableBody">
|
||
|
||
</tbody>
|
||
</table>
|
||
</div> <!-- /.card-body -->
|
||
</div> <!-- /.card -->
|
||
</div> <!-- /.row -->
|
||
<div class="col-md-9">
|
||
<div class="card">
|
||
<div class="card-body table-responsive p-0" style="height: 85vh; padding-left: 0;">
|
||
<div id="map">
|
||
<div id="popup"></div>
|
||
</div>
|
||
</div> <!-- /.card-body -->
|
||
</div> <!-- /.card -->
|
||
</div> <!-- /.row -->
|
||
</div><!-- /.container-fluid -->
|
||
</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(['{{lon}}', '{{lat}}']), // Центр карты
|
||
zoom: 12,
|
||
}),
|
||
});
|
||
|
||
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) {
|
||
console.log(property)
|
||
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, 2000)
|
||
}
|
||
|
||
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('update_object/', {
|
||
method: 'POST',
|
||
headers: {'X-CSRFToken': csrftoken},
|
||
mode: 'same-origin',
|
||
})
|
||
.then(response => response.json())
|
||
.then(data => {
|
||
const workersTableBody = document.querySelector('#workersTableBody')
|
||
const workersCount = document.querySelector('#workersCount')
|
||
workersCount.textContent = `( ${data.length} )`
|
||
workersTableBody.innerHTML = ''
|
||
data.forEach(worker => {
|
||
let tableRow = createWorkersTableRow(worker)
|
||
workersTableBody.appendChild(tableRow)
|
||
if (VlocationMarkers[worker.id]) {
|
||
const marker = VlocationMarkers[worker.id];
|
||
|
||
updateMarkerProperties(marker, worker)
|
||
|
||
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;
|
||
|
||
function updateMarkerProperties(marker, properties) {
|
||
for (let property in properties) {
|
||
marker.set(property, properties[property])
|
||
}
|
||
}
|
||
</script>
|
||
|
||
|
||
{% endblock script_footer %}
|