traque/static/utils.js

156 lines
4.6 KiB
JavaScript
Raw Normal View History

2024-05-22 15:54:49 +02:00
var evtsource;
var markers = [];
2024-06-02 16:14:44 +02:00
var self_marker;
var name;
var id;
2024-06-05 16:50:13 +02:00
var dbg;
var CircleIcon = L.Icon.extend({
options: {
iconSize: [20, 20],
iconAnchor: [10, 10],
popupAnchor: [0, 0]
}
});
2023-09-06 18:00:41 +02:00
var SelfIcon = L.Icon.extend({
options: {
iconSize: [20, 30],
iconAnchor: [10, 15],
popupAnchor: [0, 0]
}
});
2022-09-14 10:52:29 +02:00
var icons = [
2023-09-11 17:44:18 +02:00
new CircleIcon({ iconUrl: '/icons/0.png' }),
new CircleIcon({ iconUrl: '/icons/1.png' }),
new CircleIcon({ iconUrl: '/icons/2.png' }),
new CircleIcon({ iconUrl: '/icons/3.png' }),
new CircleIcon({ iconUrl: '/icons/4.png' }),
new CircleIcon({ iconUrl: '/icons/5.png' }),
new CircleIcon({ iconUrl: '/icons/6.png' }),
new CircleIcon({ iconUrl: '/icons/7.png' }),
new CircleIcon({ iconUrl: '/icons/8.png' }),
new CircleIcon({ iconUrl: '/icons/9.png' }),
new CircleIcon({ iconUrl: '/icons/10.png' }),
new CircleIcon({ iconUrl: '/icons/11.png' }),
2022-09-14 10:52:29 +02:00
];
2023-09-06 18:00:41 +02:00
var self_icons = [
2023-09-11 17:44:18 +02:00
new SelfIcon({ iconUrl: '/icons/self_0.png' }),
new SelfIcon({ iconUrl: '/icons/self_1.png' }),
new SelfIcon({ iconUrl: '/icons/self_2.png' }),
new SelfIcon({ iconUrl: '/icons/self_3.png' }),
new SelfIcon({ iconUrl: '/icons/self_4.png' }),
new SelfIcon({ iconUrl: '/icons/self_5.png' }),
new SelfIcon({ iconUrl: '/icons/self_6.png' }),
new SelfIcon({ iconUrl: '/icons/self_7.png' }),
new SelfIcon({ iconUrl: '/icons/self_8.png' }),
new SelfIcon({ iconUrl: '/icons/self_9.png' }),
new SelfIcon({ iconUrl: '/icons/self_10.png' }),
new SelfIcon({ iconUrl: '/icons/self_11.png' }),
2023-09-06 18:00:41 +02:00
];
2022-09-14 10:52:29 +02:00
//////////////////////////////////////////////////////////////////////////////
// INIT MAP
2022-09-14 14:09:06 +02:00
var map_border = [
[48.838991, 2.389310],
[48.865471, 2.356133],
[48.867714, 2.313811],
[48.841481, 2.307930],
[48.827912, 2.326842],
[48.821826, 2.358638],
[48.838991, 2.389310]
];
2022-09-14 10:52:29 +02:00
var map;
function setup_map(){
2024-05-22 15:54:49 +02:00
map = L.map('map').setView([48.8448, 2.3550], 13);
2022-09-14 10:52:29 +02:00
2024-05-22 15:54:49 +02:00
L.tileLayer('https://tiles.stadiamaps.com/tiles/stamen_toner/{z}/{x}/{y}{r}.{ext}', {
maxZoom: 20,
minZoom: 0,
attribution: '&copy; <a href="https://www.stadiamaps.com/" target="_blank">Stadia Maps</a> &copy; <a href="https://www.stamen.com/" target="_blank">Stamen Design</a> &copy; <a href="https://openmaptiles.org/" target="_blank">OpenMapTiles</a> &copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
ext: 'png'
}).addTo(map);
2022-09-14 14:09:06 +02:00
2024-05-22 15:54:49 +02:00
L.polyline(map_border, {color: 'red'}).addTo(map);
2024-06-10 09:43:35 +02:00
}
function setup_map_self(){
2024-06-02 16:14:44 +02:00
self_marker = L.marker([0,0], {"icon": icons[0] }).addTo(map);
self_marker.setZIndexOffset(1000);
self_marker.bindPopup(name);
2022-09-14 10:52:29 +02:00
}
//////////////////////////////////////////////////////////////////////////////
2024-05-22 15:54:49 +02:00
// EVENT LISTENNING
function setup_evtlisten_common(){
2024-06-02 16:14:44 +02:00
evtSource = new EventSource("/track/"+id+"/events");
2024-05-22 15:54:49 +02:00
evtSource.addEventListener("coords", (event) => {
const data = JSON.parse(event.data);
2024-06-05 16:50:13 +02:00
if(dbg) console.log('coords: ', data);
2024-05-22 15:54:49 +02:00
var i = 0;
for (tracked of data) {
if (i == markers.length) {
markers.push(L.marker([0,0], {"icon": icons[0] }).addTo(map));
markers[i].bindPopup("");
2024-06-02 16:14:44 +02:00
markers[i].setZIndexOffset(0);
2024-05-22 15:54:49 +02:00
}
markers[i].setLatLng(tracked.pos);
markers[i].setPopupContent(tracked.name);
2024-06-02 16:14:44 +02:00
markers[i].setIcon(icons[tracked.color]);
2024-05-22 15:54:49 +02:00
++i;
}
for (; i < markers.length; ++i) {
markers[i].setLatLng([0,0]);
}
});
2024-06-02 16:14:44 +02:00
evtSource.addEventListener("self_info", (event) => {
const data = JSON.parse(event.data);
2024-06-05 16:50:13 +02:00
if(dbg) console.log('self: ', data);
2024-06-02 16:14:44 +02:00
self_marker.setLatLng(data.pos);
self_marker.setIcon(self_icons[data.color]);
2024-06-05 16:50:13 +02:00
self_state_hook(data.state);
2024-06-02 16:14:44 +02:00
});
2024-05-22 15:54:49 +02:00
//socket.on("popup", function(data){
// alert(data.content);
//});
//socket.on("remove", function(data){
// if(data.id in markers)
// markers[data.id].remove();
//});
//socket.on("newTracker", function(data){
// L.marker(data.position, {"icon": icons[1]}).addTo(map);
//});
2022-09-14 10:52:29 +02:00
}
//////////////////////////////////////////////////////////////////////////////
// GEOLOCALISATION
function setup_geoLoc(){
2024-05-22 15:54:49 +02:00
const requestOptions = { method: 'PUT' };
function geoLoc_success(pos) {
2024-06-23 16:05:51 +02:00
fetch("/track/"+id+"/pos?lat="+pos.coords.latitude+"&long="+pos.coords.longitude, requestOptions);
2024-05-22 15:54:49 +02:00
}
2022-09-14 10:52:29 +02:00
2024-05-22 15:54:49 +02:00
function geoLoc_error(err) {
console.error(`ERROR(${err.code}): ${err.message}`);
}
2022-09-14 10:52:29 +02:00
2024-05-22 15:54:49 +02:00
var options = {
enableHighAccuracy: false,
timeout: 5000,
maximumAge: 0
};
2022-09-14 10:52:29 +02:00
2024-05-22 15:54:49 +02:00
navigator.geolocation.watchPosition(geoLoc_success, geoLoc_error, options);
2022-09-14 14:09:06 +02:00
}