-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
136 lines (122 loc) · 3.75 KB
/
index.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.19.0/firebase-app.js";
import {
getDatabase,
ref,
update,
onValue,
query,
startAt,
orderByChild,
get,
limitToLast,
} from "https://www.gstatic.com/firebasejs/9.19.0/firebase-database.js";
const firebaseConfig = {
databaseURL:
"https://realtimelocationtracker-emre-default-rtdb.europe-west1.firebasedatabase.app/",
};
const app = initializeApp(firebaseConfig);
const database = getDatabase(app);
const nickName = document.getElementById("nickname");
const addBtn = document.getElementById("addBtn");
const showList = document.getElementById("showList");
const locationList = document.getElementById("locationList");
const arrow = document.getElementById("arrow");
addBtn.addEventListener("click", () => {
if (nickName.value == "") {
alert("You must enter a nickname!");
} else {
if (navigator.geolocation) {
navigator.geolocation.watchPosition(setLocation);
} else {
alert("Geolocation is not supported by this browser.");
}
}
});
function setLocation(position) {
const userLocation = {
label: nickName.value,
latitude: position.coords.latitude,
longitude: position.coords.longitude,
timestamp: Date.now(),
};
const updates = {};
updates["/locations/" + nickName.value] = userLocation;
update(ref(database), updates);
console.log(`${nickName.value} updated`);
getLocations();
}
let markers = [];
let map;
async function initMap() {
const { Map } = await google.maps.importLibrary("maps");
map = new Map(document.getElementById("locations"), {
center: { lat: 41.2797, lng: 36.3361 }, // Samsun, TR
zoom: 12,
});
}
window.initMap = initMap();
function getLocations() {
const now = Date.now();
const oneMinuteAgo = now - (10 * 1000);
console.log("zaman => ", now)
const onlyOnline = query(ref(database, "locations"), orderByChild('timestamp'), startAt(oneMinuteAgo));
onValue(onlyOnline, (snapShot) => {
const data = snapShot.val();
if(data){
const infoWindow = new google.maps.InfoWindow(); // Marker Info
removeMarkers();
markers = [];
Object.values(data).map((locData) => {
const newMarker = new google.maps.Marker({
position: new google.maps.LatLng(locData.latitude, locData.longitude),
map: map,
icon: "img/navigation.png",
title: locData.label,
optimized: false,
});
newMarker.addListener("click", () => {
infoWindow.close();
infoWindow.setContent(newMarker.getTitle());
infoWindow.open(newMarker.getMap(), newMarker);
});
markers.push(newMarker);
});
showMarkers();
}else{
console.log("uygun veri yok.")
}
});
}
const setMapOnAll = (map) => {
for (let i = 0; i < markers.length; i++) {
markers[i].setMap(map);
}
};
const showMarkers = () => {
setMapOnAll(map);
};
const removeMarkers = () => {
setMapOnAll(null);
markers = [];
};
showList.addEventListener('click', ()=>{
if(locationList.classList.value === "hide"){
locationList.className = "show"
arrow.className = "arrow up"
get(query(ref(database, 'locations'),orderByChild('timestamp'), limitToLast(7))).then((snapshot) => {
if (snapshot.exists()) {
locationList.innerHTML = Object.values(snapshot.val()).map((location)=>{
return `<li><b>${location.label}</b> - ${location.latitude.toString().slice(0, 6)} ° N, ${location.longitude.toString().slice(0, 6)} ° E</li>`
}).join('');
} else {
console.log("No data available");
}
}).catch((error) => {
console.error(error);
});
}else{
locationList.className = "hide"
arrow.className = "arrow down"
}
});
console.log("Init database => ", database);