in this javascript for Leaflet I am fetching building outlines from the overpass api.…
- User
in this javascript for Leaflet I am fetching building outlines from the overpass api. Can I fill the building outlines 'let money = 100000;
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) { // Update building radius and city coordinates let buildingRadius = 300; let firstCityCoords = [e.latlng.lat, e.latlng.lng];
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) => { // 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 polyline for the road const polyline = L.polyline( coordinates, { color: 'black', // Set road color weight: 2, // Set road weight opacity: 1, // Set road opacity } ).addTo(map); } }); }) .catch((error) => { console.error('Error fetching data:', error); });});
'
- Assistant
Yes, you can fill the building outlines by using the L.polygon() method instead of L.polyline(). Here is an example of how you can modify your code to fill the building outlines:
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) => { // 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: 'green', // Set building fill color fillOpacity: 0.5, // Set building fill opacity } ).addTo(map); } }); }) .catch((error) => { console.error('Error fetching data:', error); }); - User
once i have added individual buildings to the map can I make them selectable
- Assistant
Yes, you can make the individual buildings selectable on the map by adding event listeners to the polygon objects. Here's an example of how you can make the buildings selectable on the map:
.then((data) => { // 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: 'green', // Set building fill color fillOpacity: 0.5, // Set building fill opacity } ).addTo(map); // Add click event listener to the building polygon polygon.on('click', function(e) { // Deselect all buildings map.eachLayer(function(layer) { layer.setStyle({fillOpacity: 0.5}); }); // Highlight the selected building e.target.setStyle({fillOpacity: 1}); }); } }); }) - User
this gives an error 'layer.setStyle is not a function'
- Assistant
I apologize for the error. To make individual buildings selectable on the map without using the
setStylemethod, you can maintain a reference to the selected building and update its style separately. Here's an example to achieve this:let selectedBuilding = null; .then((data) => { // 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 building = L.polygon( coordinates, { color: 'black', // Set building outline color weight: 2, // Set building outline weight fill: true, // Fill the building outline fillColor: 'green', // Set building fill color fillOpacity: 0.5, // Set building fill opacity } ).addTo(map); // Add click event listener to the building polygon building.on('click', function(e) { // Deselect the previously selected building if (selectedBuilding) { selectedBuilding.setStyle({ fillOpacity: 0.5 }); } // Highlight the clicked building building.setStyle({ fillOpacity: 1 }); selectedBuilding = building; }); } }); })