;
const coords = [];
async function geocode(address) {
const url = 'https://nominatim.openstreetmap.org/search?format=json&q=' + encodeURIComponent(address);
const response = await fetch(url, {
headers: { 'User-Agent': 'rag-app/1.0' }
});
const data = await response.json();
return data.length > 0 ? [parseFloat(data[0].lat), parseFloat(data[0].lon)] : null;
}
for (const addr of addresses) {
const point = await geocode(addr);
if (point) coords.push(point);
}
if (coords.length < 2) {
document.getElementById("map").innerHTML = "Δεν βρέθηκαν αρκετές διευθύνσεις.";
return;
}
const map = L.map("map").setView(coords[0], 12);
L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", {
attribution: "© OpenStreetMap contributors"
}).addTo(map);
const polyline = L.polyline(coords, { color: "red", weight: 4 }).addTo(map);
map.fitBounds(polyline.getBounds());
coords.forEach((point, idx) => {
L.marker(point).addTo(map).bindPopup("Στάση #" + idx + "
" + addresses[idx]);
});
const orsKey = '5b3ce3597851110001cf62488771feb1c3cf4d279e70afc1575cba37';
fetch("https://api.openrouteservice.org/v2/directions/driving-car", {
method: "POST",
headers: {
"Authorization": orsKey,
"Content-Type": "application/json"
},
body: JSON.stringify({
coordinates: coords.map(c => [c[1], c[0]]),
instructions: false
})
})
.then(res => res.json())
.then(data => {
const km = (data.routes[0].summary.distance / 1000).toFixed(2);
const mins = Math.round(data.routes[0].summary.duration / 60);
document.getElementById("route-info").innerText = `🛣️ Απόσταση: ${km} km – 🕒 ~${mins} λεπτά`;
});
});