2022-09-14 10:52:29 +02:00
|
|
|
var protocol = location.protocol;
|
|
|
|
var server = location.hostname;
|
|
|
|
var port = location.port;
|
|
|
|
var socket = io.connect({rejectUnauthorized: false},
|
|
|
|
protocol+"://"+server+":"+port);
|
|
|
|
|
|
|
|
var id = -1;
|
|
|
|
var names = [];
|
|
|
|
var colors = [];
|
|
|
|
var markers = [];
|
2022-09-14 15:48:08 +02:00
|
|
|
|
|
|
|
var CircleIcon = L.Icon.extend({
|
|
|
|
options: {
|
|
|
|
iconSize: [20, 20],
|
|
|
|
iconAnchor: [10, 10],
|
|
|
|
popupAnchor: [0, 0]
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-09-14 10:52:29 +02:00
|
|
|
var icons = [
|
2022-09-14 15:48:08 +02:00
|
|
|
new CircleIcon({ iconUrl: 'def.png' }),
|
|
|
|
new CircleIcon({ iconUrl: 'track.png' }),
|
|
|
|
new CircleIcon({ iconUrl: 'purple.png' }),
|
|
|
|
new CircleIcon({ iconUrl: 'green.png' }),
|
|
|
|
new CircleIcon({ iconUrl: 'orange.png' })
|
2022-09-14 10:52:29 +02:00
|
|
|
];
|
|
|
|
var self_icons = icons;
|
2022-09-14 15:48:08 +02:00
|
|
|
var self_invisible = new CircleIcon({ iconUrl: 'invi.png' });
|
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(){
|
|
|
|
map = L.map('map').setView([48.8448, 2.3550], 13);
|
|
|
|
|
2022-09-14 14:14:52 +02:00
|
|
|
L.tileLayer('https://stamen-tiles.a.ssl.fastly.net/toner/{z}/{x}/{y}.png', {
|
2022-09-14 10:52:29 +02:00
|
|
|
maxZoom: 19,
|
|
|
|
attribution: '© OpenStreetMap'
|
|
|
|
}).addTo(map);
|
2022-09-14 14:09:06 +02:00
|
|
|
|
|
|
|
L.polyline(map_border, {color: 'red'}).addTo(map);
|
2022-09-14 10:52:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
// UPDATE MAP
|
|
|
|
|
|
|
|
function setup_socket_common(){
|
|
|
|
socket.on("moving", function(data){
|
|
|
|
console.log("moving", data);
|
|
|
|
if(!(data.id in markers)){
|
|
|
|
if(!(data.id in colors))
|
|
|
|
colors[data.id] = 0;
|
|
|
|
var icon;
|
|
|
|
if(data.id == id)
|
|
|
|
icon = self_icons[colors[data.id]];
|
|
|
|
else
|
|
|
|
icon = icons[colors[data.id]];
|
|
|
|
markers[data.id] = L.marker(data.position, {"icon": icon}).addTo(map);
|
|
|
|
if(data.id == id)
|
|
|
|
markers[data.id].setZIndexOffset(10000);
|
|
|
|
if(data.id in names)
|
|
|
|
markers[data.id].bindPopup(names[data.id]);
|
|
|
|
} else{
|
|
|
|
markers[data.id].setLatLng(data.position);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on("popup", function(data){
|
|
|
|
alert(data.content);
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on("setName", function(data){
|
|
|
|
console.log("setName", data);
|
|
|
|
names[data.id] = data.name;
|
|
|
|
if(data.id in markers)
|
|
|
|
markers[data.id].bindPopup(data.name);
|
|
|
|
});
|
2022-09-14 14:14:52 +02:00
|
|
|
|
|
|
|
socket.on("remove", function(data){
|
|
|
|
if(data.id in markers)
|
|
|
|
markers[data.id].remove();
|
|
|
|
});
|
2022-09-14 14:45:51 +02:00
|
|
|
|
|
|
|
socket.on("newTracker", function(data){
|
|
|
|
L.marker(data.position, {"icon": icons[1]}).addTo(map);
|
|
|
|
});
|
2022-09-14 10:52:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function setup_socket_not_admin(){
|
|
|
|
socket.on("yourId", function(data){
|
|
|
|
console.log("yourId", data);
|
|
|
|
id = data.id;
|
|
|
|
if(!(id in colors))
|
|
|
|
colors[id] = 0;
|
|
|
|
if(id in markers)
|
|
|
|
markers[id].setIcon(self_icons[colors[id]]);
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on("changeColor", function(data){
|
|
|
|
console.log("changeColor", data);
|
|
|
|
if(data.id in colors && colors[data.id] == -1 && data.id in markers)
|
|
|
|
markers[data.id].addTo(map);
|
|
|
|
colors[data.id] = data.color;
|
|
|
|
if(data.id in markers){
|
|
|
|
if(data.id == id)
|
|
|
|
if(data.color == -1)
|
|
|
|
markers[data.id].setIcon(self_invisible);
|
|
|
|
else
|
|
|
|
markers[data.id].setIcon(self_icons[data.color]);
|
|
|
|
else
|
|
|
|
if(data.color == -1)
|
|
|
|
markers[data.id].setLatLng([0,0]);
|
|
|
|
else
|
|
|
|
markers[data.id].setIcon(icons[data.color]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
// GEOLOCALISATION
|
|
|
|
|
|
|
|
function setup_geoLoc(){
|
|
|
|
function geoLoc_success(pos) {
|
|
|
|
socket.emit("geoLoc", {"position": [pos.coords.latitude, 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);
|
2022-09-14 14:09:06 +02:00
|
|
|
}
|