I am retrieving data from the overpass api with this javascript ' const overpassQuery =…
- User
I am retrieving data from the overpass api with this javascript ' const overpassQuery =
[out:json]; node["cuisine"="coffee_shop"](around:1500,${firstCityCoords[1]},${firstCityCoords[0]}); out;;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 console.log(data); }) .catch(error => { console.error('Error fetching data:', error); }); ' how can I add markers to a leaflet.js map using the fetched data
- Assistant
After fetching the data from the Overpass API, you can add markers to a Leaflet.js map using the data by iterating over the results and adding markers for each node. Here's an example of how you can do this:
// Assuming you have initialized a Leaflet.js map with 'map' variable 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 === 'node') { // Create a marker for each node element const marker = L.marker([element.lat, element.lon]).addTo(map); // You can add popups or customize markers here marker.bindPopup('Name: ' + element.tags.name); } }); }) .catch(error => { console.error('Error fetching data:', error); });In this code snippet, we iterate over the elements returned by the Overpass API and create markers for each node with the latitude and longitude provided. You can customize the markers by adding popups, colors, icons, or any other Leaflet.js marker options.