-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
45 lines (37 loc) · 1.07 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
const socket = io();
if (navigator.geolocation) {
navigator.geolocation.watchPosition(
(position) => {
const { latitude, longitude } = position.coords;
socket.emit("send-location", { latitude, longitude });
},
(error) => {
console.error(error);
},
{
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0,
}
);
}
const map = L.map("map").setView([0, 0],16); // Added initial zoom level
L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", {
attribution: "Tushar Verma",
}).addTo(map);
const markers = {};
socket.on("recieve-location", (data) => {
const { id, latitude, longitude } = data;
map.setView([latitude, longitude]);
if (markers[id]) {
markers[id].setLatLng([latitude, longitude]);
} else {
markers[id] = L.marker([latitude, longitude]).addTo(map);
}
});
socket.on("user-disconnected", (id) => {
if (markers[id]) {
map.removeLayer(markers[id]);
delete markers[id];
}
});