show bus and passenger count in layerControl

This commit is contained in:
steven 2025-03-26 23:19:04 -04:00
parent cbbf56feed
commit b7e2fb226e
2 changed files with 27 additions and 29 deletions

View file

@ -5,7 +5,7 @@
<meta charset="UTF-8">
<meta content="TARC Routes" property="og:title">
<meta content='https://tarc.stevenalexander.org/bus.svg' property='og:image'>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="viewport" content="width=device-width,initial-scale=1">
<link rel="icon" type="image/x-icon" href="/bus.svg">
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js" integrity="sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo=" crossorigin=""></script>
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css" integrity="sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY=" crossorigin="" />
@ -14,35 +14,37 @@
<body>
<div id="map"></div>
<script>
let map=L.map("map",{center:[38.21,-85.75],zoom:13,layers:[L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png")]});
let map=L.map("map",{center:[38.2,-85.75],zoom:13,layers:[L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png")]});
let layerControl=L.control.layers().addTo(map);
let busLayers={};busMarkers={};
let busLayers={},busMarkers={};
function updateMap(){
let routes={};
fetch("tarc.csv").then(response=>response.text()).then(data=>{
if(data=="")return;
let newRoute=false;
data.split("\n").forEach(line=>{
let [lat,lng,r,id,p,d,s,t]=line.split(",");
let info=`<b>Route ${r}</b><br>#${id}<br>${p} riders<br>${d}<br>${s}mph<br>${new Date(t*1000).toLocaleTimeString('en-US',{hour12:false})}`;
if(id in busMarkers){
busMarkers[id].setLatLng([lat,lng]).bindPopup(info)
}else{
if(!busLayers[r]){busLayers[r]=L.layerGroup().addTo(map)}
busMarkers[id]=L.marker([lat,lng]).bindPopup(info).addTo(busLayers[r]);
newRoute=true
}
p=parseInt(p);
t=new Date(t*1000).toLocaleTimeString('en-US',{hour12:false});
if(!busLayers[r]){busLayers[r]=L.layerGroup().addTo(map)}
if(!busMarkers[id]){busMarkers[id]=L.marker([0,0]).addTo(busLayers[r])}
if(!routes[r]){routes[r]=[0,0]}
routes[r][0]++;
routes[r][1]+=p;
busMarkers[id].setLatLng([lat,lng]).bindPopup(`<b>Route ${r}</b><br>#${id}<br>${p} riders<br>${d}<br>${s}mph<br>${t}`)
})
if(newRoute){
map.removeControl(layerControl);
layerControl=L.control.layers(null,busLayers).addTo(map)
let overlayLayers={};
for(let r in routes){
overlayLayers[`${r} (${routes[r][0]} vehicle(s) with ${routes[r][1]} riders)`]=busLayers[r]
}
layerControl.remove();
layerControl=L.control.layers(null,overlayLayers,{collapsed:false}).addTo(map);
setTimeout(updateMap,6000)
})
}
updateMap();
let locationMarker=L.marker([0,0]),locationRadius=L.circle([0,0]).bringToBack();
let locationLayer=L.layerGroup([locationMarker,locationRadius]).addTo(map);
let locationMarker=L.marker([0,0]).addTo(map);
let locationRadius=L.circle([0,0]).addTo(map).bringToBack();
map.locate({watch:true});
map.on("locationfound",e=>{
locationMarker.setLatLng(e.latlng).bindPopup("<b>You</b> at<br>"+new Date().toLocaleTimeString());