in this javascript for leaflet.js when a building is colored green I want to remove it…
- User
in this javascript for leaflet.js when a building is colored green I want to remove it from the polygon click event and display a message saying 'You already own this building' - 'let money = 100000; let numberOfBuildings = 0; let dailybonus = 0; let polygonClicked = false; // Flag to track if a polygon was clicked
const moneyElement = document.getElementById("moneydisplay"); moneyElement.textContent =
£${money};const map = L.map("map").setView([51.5352028, 0.0054299], 17);
// fetch house data
// Event listener for when the map is clicked map.on("click", function (e) { if (!polygonClicked) { // Update building radius and city coordinates let buildingRadius = 300; let firstCityCoords = [e.latlng.lat, e.latlng.lng];
if (money >= 100000) { // Code to execute when money is 100,000 or more (original code goes here) money -= 50000;
const overpassQuery = ` [out:json]; way"building"="house"; out body;
; out skel qt; `;
fetch("https://overpass-api.de/api/interpreter", { method: "POST", headers: { "Content-Type": "application/x-www-form-urlencoded", }, body: "data=" + encodeURIComponent(overpassQuery), }) .then((response) => response.json()) .then((data) => { // Update money display after successful building placement const moneyElement = document.getElementById("moneydisplay"); moneyElement.textContent =
£${money};numberOfBuildings = data.elements.length; // Get the length of the array after fetching data dailybonus += numberOfBuildings; console.log("Daily bonus total now:", dailybonus);
// Process the data returned by the Overpass API data.elements.forEach((element) => { if (element.type === "way") { // Extract coordinates from the way element const coordinates = element.nodes.map((nodeId) => { const node = data.elements.find((node) => node.id === nodeId); return [node.lat, node.lon]; });
// Create a polygon for the building const polygon = L.polygon(coordinates, { color: "black", // Set building outline color weight: 2, // Set building outline weight fill: true, // Fill the building outline fillColor: "gray", // Set building fill color fillOpacity: 0.5, // Set building fill opacity }).addTo(map);
polygon.on("click", function (e) { // Handle click event on the building footprint console.log("Building footprint clicked!"); e.originalEvent.stopPropagation(); polygonClicked = true; // Set flag to true when a polygon is clicked
- Assistant
To remove a building polygon that is colored green from the click event and display a message saying 'You already own this building', you can add an additional condition to the click event handler for the polygons. Here's how you can modify the code:
// Modify the polygon click event handler to check if the building is already owned polygon.on("click", function (e) { // Check if the building is already owned (colored green) if (polygon.options.fillColor === "green") { // Display message indicating that the building is already owned const messageDisplay = document.getElementById("messageDisplay"); messageDisplay.innerHTML = `You already own this building`; // Stop further execution return; } console.log("Building footprint clicked!"); e.originalEvent.stopPropagation(); polygonClicked = true; // Set flag to true when a polygon is clicked if (money >= 10000) { // Change polygon fill color to green polygon.setStyle({ fillColor: "green" }); // Display message after creating polygons (uses the updated numberOfBuildings) const messageDisplay = document.getElementById("messageDisplay"); messageDisplay.innerHTML = `Congratulations you have bought this building for £10,000. You will earn £1000 per day in rent.`; money -= 10000; dailybonus += 1000; const moneyDisplay = document.getElementById("moneydisplay"); const moneyString = `£${money}`; moneyDisplay.textContent = moneyString; console.log("Daily bonus total now:", dailybonus); } else { const messageDisplay = document.getElementById("messageDisplay"); messageDisplay.innerHTML = `Sorry you need £10,000 to buy this building`; } }); - User
- Assistant
In your code, you are setting the
textContentproperty of themessageDisplayelement to display the message when a green polygon is clicked. To display HTML content and format the message correctly, you should set theinnerHTMLproperty instead oftextContent.Here is the updated code snippet where the message is displayed correctly as 'You already own this building.' when a green polygon is clicked:
polygon.on("click", function (e) { if (polygon.options.fillColor === 'green') { const messageDisplay = document.getElementById("messageDisplay"); messageDisplay.innerHTML = `You already own this building.`; // Stop further execution return; } console.log("Building footprint clicked!"); e.originalEvent.stopPropagation(); polygonClicked = true; // Set flag to true when a polygon is clicked if (money >= 10000) { polygon.setStyle({ fillColor: "green" }); const messageDisplay = document.getElementById("messageDisplay"); messageDisplay.innerHTML = `Congratulations you have bought this building for £10,000. You will earn £1000 per day in rent.`; money -= 10000; dailybonus += 1000; const moneyDisplay = document.getElementById("moneydisplay"); const moneyString = `£${money}`; moneyDisplay.textContent = moneyString; console.log("Daily bonus total now:", dailybonus); } else { const messageDisplay = document.getElementById("messageDisplay"); messageDisplay.innerHTML = `Sorry you need £10,000 to buy this building`; } }); - User
no it still displays 'Sorry you don't have enough money. You need at least £100,000 to buy land.'. so when the user clicks on a green polygon the map.on click is being called
- Assistant
To prevent the
map.on("click")event from being triggered when clicking on a green polygon, you can use thee.stopPropagation()method within the green polygon click event handler to stop the event from bubbling up to the map click event. Here's how you can modify your code:polygon.on("click", function (e) { if (polygon.options.fillColor === 'green') { const messageDisplay = document.getElementById("messageDisplay"); messageDisplay.innerHTML = `You already own this building.`; // Stop the click event from propagating to the map object e.originalEvent.stopPropagation(); return; } console.log("Building footprint clicked!"); e.originalEvent.stopPropagation(); polygonClicked = true; // Set flag to true when a polygon is clicked if (money >= 10000) { polygon.setStyle({ fillColor: "green" }); const messageDisplay = document.getElementById("messageDisplay"); messageDisplay.innerHTML = `Congratulations you have bought this building for £10,000. You will earn £1000 per day in rent.`; money -= 10000; dailybonus += 1000; const moneyDisplay = document.getElementById("moneydisplay"); const moneyString = `£${money}`; moneyDisplay.textContent = moneyString; console.log("Daily bonus total now:", dailybonus); } else { const messageDisplay = document.getElementById("messageDisplay"); messageDisplay.innerHTML = `Sorry you need £10,000 to buy this building`; } });