traque/static/utils.js
2023-09-11 17:44:18 +02:00

136 lines
3.8 KiB
JavaScript

var protocol = location.protocol;
var server = location.hostname;
var port = location.port;
var socket;
var id;
var markers = {};
var CircleIcon = L.Icon.extend({
options: {
iconSize: [20, 20],
iconAnchor: [10, 10],
popupAnchor: [0, 0]
}
});
var SelfIcon = L.Icon.extend({
options: {
iconSize: [20, 30],
iconAnchor: [10, 15],
popupAnchor: [0, 0]
}
});
var icons = [
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' }),
];
var self_icons = [
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' }),
];
//////////////////////////////////////////////////////////////////////////////
// INIT MAP
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]
];
var map;
function setup_map(){
map = L.map('map').setView([48.8448, 2.3550], 13);
L.tileLayer('https://stamen-tiles.a.ssl.fastly.net/toner/{z}/{x}/{y}.png', {
maxZoom: 19,
attribution: '© OpenStreetMap'
}).addTo(map);
L.polyline(map_border, {color: 'red'}).addTo(map);
}
//////////////////////////////////////////////////////////////////////////////
// SOCKET
function setup_socket_common(){
socket.on("moving", function(data){
console.log("moving", data);
if(!(data.id in markers)){
if(data.id == id){
markers[data.id] = L.marker(data.position, {"icon": self_icons[data.color]}).addTo(map);
markers[data.id].setZIndexOffset(10000);
}
else
markers[data.id] = L.marker(data.position, {"icon": icons[data.color]}).addTo(map);
markers[data.id].bindPopup(data.id);
} else{
markers[data.id].setLatLng(data.position);
if(data.id == id)
markers[data.id].setIcon(self_icons[data.color]);
else
markers[data.id].setIcon(icons[data.color]);
}
});
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);
});
}
//////////////////////////////////////////////////////////////////////////////
// GEOLOCALISATION
function setup_geoLoc(){
function geoLoc_success(pos) {
fetch("/log?id="+id+"&lat="+pos.coords.latitude+"&lon="+pos.coords.longitude);
}
function geoLoc_error(err) {
console.error(`ERROR(${err.code}): ${err.message}`);
}
var options = {
enableHighAccuracy: false,
timeout: 5000,
maximumAge: 0
};
navigator.geolocation.watchPosition(geoLoc_success, geoLoc_error, options);
}