ruscomap/ruscoapp/public/index.js

128 lines
4.1 KiB
JavaScript
Raw Normal View History

2024-03-29 19:33:49 +01:00
let map = null;
let points = [];
2024-02-16 20:08:20 +01:00
//BOLOGNA
2024-03-29 19:33:49 +01:00
const x = 11.343095841047235;
const y = 44.49373478876318;
const piazzaMaggiore = [x, y];
2024-03-29 19:33:49 +01:00
var lat = 0;
var lng = 0;
2024-02-16 20:08:20 +01:00
// COORDS ESISTENTI
2024-03-29 19:33:49 +01:00
async function downloadRusco() {
const resp = await window.fetch("/rusco.geojson");
const rusco = await resp.json();
return rusco.features;
2024-02-16 20:08:20 +01:00
}
// SET VIEW
2024-03-29 19:33:49 +01:00
async function initMap() {
map = L.map("map").setView([44.49593478876318, 11.343095841047235], 13.5);
2024-02-16 20:08:20 +01:00
// IMPOSTA MAPPA
2024-03-29 19:33:49 +01:00
L.tileLayer("https://tile.openstreetmap.org/{z}/{x}/{y}.png", {
attribution: "© OpenStreetMap contributors | RuscoMap",
}).addTo(map);
2024-03-29 19:33:49 +01:00
loadMarkers();
map.on("click", function (e) {
lat = e.latlng.lat;
lng = e.latlng.lng;
document.getElementById("latitudeValue").textContent = lat;
document.getElementById("longitudeValue").textContent = lng;
// Bind a popup to the marker
2024-03-29 19:33:49 +01:00
showModal("modalAddMarker");
2024-02-16 20:08:20 +01:00
});
2024-03-29 19:33:49 +01:00
setInterval(loadMarkers, 60000);
}
async function loadMarkers() {
fetch("http://localhost:3000/getRuschi")
.then((response) => response.json())
.then((data) => {
console.log(data);
data.forEach((rusco) => {
map.addMarker(rusco);
});
// L.geoJSON(data, {
// onEachFeature: function (feature, layer) {
// layer.bindPopup('<h2>' +
// feature.properties.Nome + '</h2><p>' + feature.properties.Descrizione + '</p><img src="' + feature.properties.Foto + '" alt="Photo" style="width:200px;height:auto;">');
// }
// }).addTo(map);
// });
});
2024-02-16 20:08:20 +01:00
}
2024-02-16 20:08:20 +01:00
// SEGUENZA EXEC
2024-03-29 19:33:49 +01:00
async function load() {
points = await downloadRusco();
await initMap();
2024-02-16 20:08:20 +01:00
}
2024-02-25 20:17:10 +01:00
2024-03-29 19:33:49 +01:00
function showModal(modalName) {
const selectedModal = document.getElementById(modalName);
const modalOverlay = document.getElementById("modalOverlay");
selectedModal.style.display = "block"; // Show the modal
modalOverlay.style.display = "block"; // Show the overlay
2024-02-25 20:17:10 +01:00
}
2024-03-29 19:33:49 +01:00
function detectCurrentShownModal() {
const modalAddMarker = document.getElementById("modalAddMarker");
const modalSign = document.getElementById("modalSign");
return modalAddMarker.style.display !== "none" ? modalAddMarker : modalSign;
2024-02-25 20:17:10 +01:00
}
2024-03-29 19:33:49 +01:00
function closeModal() {
const currentShownModal = detectCurrentShownModal();
const modalOverlay = document.getElementById("modalOverlay");
currentShownModal.style.display = "none";
modalOverlay.style.display = "none";
}
2024-02-25 20:17:10 +01:00
2024-03-29 19:33:49 +01:00
function addMarker() {
const name = document.getElementById("name").value;
const description = document.getElementById("description").value;
const imageUrl = document.getElementById("photo").value;
const latitude = document.getElementById("latitude").value;
const longitude = document.getElementById("longitude").value;
fetch("http://localhost:3000/addRuschi", {
method: "POST",
headers: {
2024-03-29 19:33:49 +01:00
"Content-Type": "application/json",
},
body: JSON.stringify({
name: name,
description: description,
imageUrl: imageUrl,
latitude: latitude,
2024-03-29 19:33:49 +01:00
longitude: longitude,
}),
})
.then((response) => {
if (response.ok) {
// Authentication successful
2024-03-29 19:33:49 +01:00
console.log("Added marker");
// Redirect the user to the authenticated area or perform other actions
} else {
// Authentication failed
2024-03-29 19:33:49 +01:00
console.error("Error adding marker");
}
})
2024-03-29 19:33:49 +01:00
.catch((error) => {
console.error("Error adding marker", error);
});
closeModalAddMarker();
2024-02-25 20:17:10 +01:00
}
2024-03-29 19:33:49 +01:00
window.addEventListener("load", load);
2024-02-25 20:17:10 +01:00
2024-03-29 19:33:49 +01:00
const showAddMarkerModal = document.getElementById("showAddMarkerModal");
showAddMarkerModal.addEventListener("click", () => showModal("modalAddMarker"));
2024-02-25 20:17:10 +01:00
2024-03-29 19:33:49 +01:00
const showModalSign = document.getElementById("showModalSign");
showModalSign.addEventListener("click", () => showModal("modalSign"));
2024-02-25 20:17:10 +01:00
2024-03-29 19:33:49 +01:00
const modalOverlay = document.getElementById("modalOverlay");
modalOverlay.addEventListener("click", closeModal);