-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
53 lines (45 loc) · 1.69 KB
/
script.js
File metadata and controls
53 lines (45 loc) · 1.69 KB
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
43
44
45
46
47
48
49
50
51
52
53
// console.log("hello");
const socket = io("https://map-backend-knsb.onrender.com");
const geoOptions = {
timeout: 5000,
enableHighAccuracy: true
};
let map;
let markers = new Map();
if (navigator.geolocation) {
navigator.geolocation.watchPosition(
(position) => {
const { latitude, longitude } = position.coords;
socket.emit("current_position", { latitude, longitude });
if (!map) {
map = L.map('map').setView([latitude, longitude], 13);
L.tileLayer(`https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png?v=${new Date().getTime()}`, {
maxZoom: 19,
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
} else {
map.setView([latitude, longitude], 13);
}
},
(error) => {
console.error("Error getting location: ", error);
alert("Unable to retrieve your location. Using default location.");
},
geoOptions
);
socket.on("recieve_location", (data) => {
// console.log("Received location data:", data);
data.forEach(userData => {
const { socketId, latitude, longitude } = userData;
if (markers[socketId]) {
markers[socketId].setLatLng([latitude, longitude]);
} else {
const marker = L.marker([latitude, longitude]).addTo(map);
marker.bindPopup(`User ID: ${socketId}`);
markers.set(socketId, marker);
}
});
});
} else {
alert("Geolocation is not supported by this browser.");
}