-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
44 lines (35 loc) · 981 Bytes
/
script.js
File metadata and controls
44 lines (35 loc) · 981 Bytes
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("sendLocation", { latitude, longitude })
}, (error) => {
console.log(error)
},
{
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0
}
)
}
const map = L.map("map").setView([0,0], 16);
L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", {
attribution: "OpenStreetMap"
}).addTo(map);
const marjer = {}
socket.on("newLocation", (data) => {
const { id, latitude, longitude } = data
map.setView([latitude, longitude]);
if(marjer[id]){
marjer[id].setLatLng([latitude, longitude])
} else {
marjer[id] = L.marker([latitude, longitude]).addTo(map)
}
})
socket.on("userDisconnected", (id) => {
if(marjer[id]){
map.removeLayer(marjer[id])
delete marjer[id]
}
})