Is there any way to disable the building area on the app using OpenStreetMap, Leaflet?
Thank you
map.on(“click”, debounce(function (e) {
const { lat, lng } = e.latlng;
console.log(Map clicked at: ${lat}, ${lng}
); // Log click coordinates
getSurfaceType(lat, lng)
.then(data => {
console.log(“Surface Type API Response:”, data); // Log the response data
// Proceed with getting the nearest road even if surface type data is null
return getNearestRoad(lat, lng);
})
.then(nearestRoadData => {
console.log('Nearest road data:', nearestRoadData); // Log the nearest road data
if (nearestRoadData) {
const nearestRoad = nearestRoadData.road;
const adjustedLat = nearestRoadData.lat;
const adjustedLng = nearestRoadData.lon;
// Calculate the distance between the clicked point and the nearest road
const distance = calculateDistance(lat, lng, adjustedLat, adjustedLng);
console.log(`Distance to nearest road: ${distance} km`);
if (distance <= DISTANCE_THRESHOLD) {
// Show the modal immediately
modal.style.display = "block";
// Update the nearestHighwaySpan content asynchronously
nearestHighwaySpan.textContent = `at "${nearestRoad}"`;
// Get the modal input fields
const nameInput = document.getElementById("name");
const colorInput = document.getElementById("color");
const startTimeInput = document.getElementById("startTime");
const endTimeInput = document.getElementById("endTime");
// Handle the "Create Node" button click
const createNodeButton = document.getElementById("createNodeButton");
createNodeButton.onclick = function () {
const name = nameInput.value.trim();
const nodeColor = colorInput.value.trim();
const startTime = startTimeInput.value.trim();
const endTime = endTimeInput.value.trim();
if (!name) {
customAlert("Please fill the stop name.");
return;
} else if ((startTime && !endTime)) {
customAlert("Please fill the endTime or delete the startTime.");
return;
} else if ((endTime && !startTime)) {
customAlert("Please fill the startTime or delete the endTime.");
return;
} else if (convertTimeToSeconds(startTime) > convertTimeToSeconds(endTime)) {
customAlert("The startTime must be earlier than endTime.");
return;
}
// Close the modal
closeModal(modal);
// Fetch additional data asynchronously
fetch(`https://nominatim.openstreetmap.org/reverse?format=json&lat=${adjustedLat}&lon=${adjustedLng}&zoom=18&addressdetails=1`)
.then(response => response.json())
.then(data => {
console.log('Nominatim response for adjusted coordinates:', data); // Log response for adjusted coordinates
if (data && data.address) {
const nearestHighway = data.address.road;
const roadNumber = data.address.house_number || "";
let icon;
if (nodeColor === null) {
icon = mapConfig.redIcon;
} else if (nodeColor === "yellow") {
icon = mapConfig.yellowIcon;
} else if (nodeColor === "green") {
icon = mapConfig.greenIcon;
} else if (nodeColor === "orange") {
icon = mapConfig.orangeIcon;
} else if (nodeColor === "blue") {
icon = mapConfig.blueIcon;
} else if (nodeColor === "violet") {
icon = mapConfig.violetIcon;
} else {
icon = mapConfig.redIcon;
}
checkIfmyNodeNameIsUnique(name).then(count => {
if (count > 0) {
customAlert("This name is already taken. Please enter a different name.", 2);
} else {
// Proceed with node creation here
const session = driver.session({ database: config.neo4jDatabase });
if (name) {
const storeNodeQuery = `
MERGE (n:Node {streetName: $streetName, latitude: $latitude, longitude: $longitude, streetNumber: $streetNumber, name: $nodeName ,nodeColor: $nodeColor ,startTime: $startTime , endTime: $endTime})
`;
session
.run(storeNodeQuery, { streetName: nearestHighway, latitude: adjustedLat, longitude: adjustedLng, streetNumber: roadNumber, nodeName: name, nodeColor: nodeColor, startTime: startTime, endTime: endTime })
.then(() => {
loadNodesFromNeo4j();
console.log("Node created in Neo4j with streetName:", nearestHighway, "latitude:", adjustedLat, "longitude:", adjustedLng, "streetNumber:", roadNumber, "name:", name, "nodeColor:", nodeColor, "startTime:", startTime, "endTime:", endTime);
L.marker([adjustedLat, adjustedLng], { icon: icon })
.addTo(map)
.bindPopup(`Street: ${nearestHighway}<br>Road Number: ${roadNumber}<br>Name: ${name}<br>Node Color: ${nodeColor}<br>Start Time: ${startTime}<br>End Time: ${endTime}<br><button onclick="removeMarker(this)">Cancel</button>`);
})
.catch((error) => {
console.error("Error creating node:", error);
})
.then(() => {
session.close();
});
} else {
customAlert("You have to give a name at the stop");
}
}
})
.catch(error => {
console.error("Error:", error);
});
} else {
customAlert("No address information found at this location. Node creation is not allowed.");
}
})
.catch((error) => {
console.error("Error fetching address data:", error);
});
};
const closeButton = document.getElementsByClassName("close")[0];
closeButton.onclick = function () {
// Reset the input fields
nameInput.value = "";
colorInput.value = "";
startTimeInput.value = "";
endTimeInput.value = "";
// Close the modal
closeModal(modal);
};
// Function to close the modal
function closeModal(modal) {
modal.style.display = "none";
}
} else {
customAlert("Stop creation is only allowed within 50 meters of a road.");
}
} else {
customAlert("Stop creation is only allowed on roads.");
}
})
.catch(error => {
console.error("Error fetching nearest road:", error);
});
}, 300));