traque/map.html

136 lines
3.7 KiB
HTML
Raw Normal View History

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Leaflet - test</title>
<!-- LEAFLET INCLUDE -->
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.8.0/dist/leaflet.css"
integrity="sha512-hoalWLoI8r4UszCkZ5kL8vayOGVae1oxXe/2A4AO6J9+580uKHDO3JdHb7NzwwzK5xr/Fs0W40kiNHxM9vyTtQ=="
crossorigin=""/>
<script src="https://unpkg.com/leaflet@1.8.0/dist/leaflet.js"
integrity="sha512-BB3hKbKWOc9Ez/TAwyWxNXeoV9c1v6FIeYiBieIWkpLjauysF18NzgR1MBNBXf8/KABdlkX68nAhlwcDFLGPCQ=="
crossorigin=""></script>
<!-- SOCKET.IO INCLUDE -->
<script src="/socket.io/socket.io.js"></script>
<style type="text/css">
#map { height: 500px; }
</style>
</head>
<body>
<div id="map"></div><br/>
<input id="name"/><button id="setName">Set team name</button><br/>
2022-09-09 13:03:08 +02:00
<input type="number" id="color" value="0"/><button id="setColor">Set color</button><br/>
<script type="text/javascript">
2022-09-09 13:03:08 +02:00
var protocol = "https";
var server = "localhost";
var port = "9000";
2022-09-09 13:03:08 +02:00
var socket = io.connect({rejectUnauthorized: false},
protocol+"://"+server+":"+port);
var id = -1;
var names = {};
var colors = {};
var markers = {};
var icons = { /* TODO */ };
var self_icons = { /* TODO */ };
//////////////////////////////////////////////////////////////////////////////
// INIT MAP
var map = L.map('map').setView([48.8448, 2.3550], 13);
L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 19,
attribution: '© OpenStreetMap'
}).addTo(map);
//////////////////////////////////////////////////////////////////////////////
// UPDATE MAP
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("setName", function(data){
console.log("setName", data);
names[data.id] = data.name;
if(data.id in markers)
markers[data.id].setIcon(icons[data.color]);
});
socket.on("changeColor", function(data){
console.log("changeColor", data);
colors[data.id] = data.color;
if(data.id in markers){
if(data.id == id)
markers[data.id].setIcon(self_icons[data.color]);
else
markers[data.id].setIcon(icons[data.color]);
}
});
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);
} else
markers[data.id].setLatLng(position);
});
//////////////////////////////////////////////////////////////////////////////
// SETTINGS -- NAME AND COLOR
document.querySelector('#setName').addEventListener('click', function(){
const input = document.querySelector('#name');
socket.emit("setName", {"name": input.value});
});
document.querySelector('#setColor').addEventListener('click', function(){
const input = document.querySelector('#color');
2022-09-09 13:03:08 +02:00
socket.emit("changeColor", {"color": input.value});
});
//////////////////////////////////////////////////////////////////////////////
// GEOLOCALISATION
function geoLoc_success(pos) {
2022-09-09 13:03:08 +02:00
console.log(pos);
socket.emit("geoLoc", {"position": [pos.coords.latitude, pos.coords.longitude]});
}
function geoLoc_error(err) {
console.error(`ERROR(${err.code}): ${err.message}`);
}
2022-09-09 13:03:08 +02:00
var options = {
enableHighAccuracy: false,
timeout: 5000,
maximumAge: 0
};
id = navigator.geolocation.watchPosition(geoLoc_success, geoLoc_error, options);
</script>
</body>
2022-09-09 13:03:08 +02:00
</html>