actualiza la posicion de buses cada 30 segundos

francisco/photos
Francisco Sandoval 2023-12-12 23:12:05 -03:00
parent 8b0b209d59
commit add1eadaae
1 changed files with 26 additions and 6 deletions

View File

@ -36,8 +36,15 @@
let ver_paraderos = false
let markers_paraderos = []
let markers_buses = []
let timeInterval = null
onMount(() => { create_map() })
onMount(() => {
create_map()
return () => {
timeInterval && globalThis.clearInterval(timeInterval)
}
})
function create_map() {
if (!elMap || !globalThis.L) return;
@ -95,6 +102,7 @@
async function onMostrarRuta(id_operador, id_linea) {
try {
loading = true
timeInterval && globalThis.clearInterval(timeInterval)
polyline && polyline.remove()
marker1 && marker1.remove()
marker2 && marker2.remove()
@ -142,13 +150,21 @@
}
async function onMostrarBuses(ver_buses) {
// 1. eliminar marcadores anteriores
markers_buses.forEach(marker => marker.remove())
if (!ver_buses || !id_linea) return;
if (!ver_buses && timeInterval) {
globalThis.clearInterval(timeInterval)
timeInterval = null
}
if (!ver_buses || !id_linea) {
markers_buses.forEach(marker => marker.remove())
return;
}
const buses = await getBusesLinea(id_linea)
console.log({ buses })
// 1. eliminar marcadores anteriores
markers_buses.forEach(marker => marker.remove())
// 2. crear marcadores
for (let mark of buses) {
@ -158,7 +174,11 @@
marker.bindTooltip(html)
markers_buses.push(marker)
console.log({ marker })
}
// actualizar posicion de buses cada 30 segundos
if (!timeInterval) {
timeInterval = globalThis.setInterval(() => onMostrarBuses(true), 30000)
}
}