ruscomap/public/js/map.js
2024-09-21 01:31:23 +02:00

46 lines
1,001 B
JavaScript

import { initMarkers, createMarker } from "./markerHandler.js";
import { initEvents } from "./eventHandler.js";
import { mapInit } from "./mapInit.js";
let editMode = false;
let lastUpdateCheck = Date.now();
const map = mapInit();
initEvents(map);
async function fetchAllMarkers() {
const response = await fetch("/fetchMarkers");
let markers = await response.json();
console.log(markers);
markers.forEach((marker) => {
createMarker(marker, L, map);
});
}
export async function fetchNewMarkers() {
const searchParams = new URLSearchParams({ fromDate: lastUpdateCheck });
const response = await fetch(`/updateMarkers?${searchParams.toString()}`);
const markers = await response.json();
markers.forEach((marker) => {
createMarker(marker, L, map);
});
lastUpdateCheck = Date.now();
}
const main = async () => {
await fetchAllMarkers();
setInterval(fetchNewMarkers, 5000);
};
main()
.then(() => {
console.log("Completed");
})
.catch(console.error);