Файл server js: const express = require(‘express’); const app = express(); const axios =…
- User
Файл server js: const express = require(‘express’); const app = express(); const axios = require(‘axios’); const path = require(‘path’);
const port = 3000; const shapes = [];
app.use(express.static(path.join(__dirname, ‘public’))); app.use(‘/leaflet’, express.static(path.join(__dirname, ‘node_modules/leaflet/dist’))); app.use(‘/leaflet.draw’, express.static(path.join(__dirname, ‘node_modules/leaflet-draw/dist’)));
app.get(‘/’, (req, res) => { res.sendFile(path.join(__dirname, ‘views’, ‘index.html’)); });
app.get(‘/data/:lat/:lng’, async (req, res) => { const latitude = req.params.lat; const longitude = req.params.lng; try { const response = await axios.get(https://overpass-api.de/api/interpreter?data=[out:json];node(around:100,${longitude});out;); res.json(response.data.elements && response.data.elements[0] ? response.data.elements[0].tags : {}); } catch (error) { console.log(error); res.status(500).json({ error: ‘An error occurred’ }); } });
app.get(‘/getShapes’, (req, res) => { res.json(shapes); });
app.get(‘/getShapesByButton/:type’, (req, res) => { const type = req.params.type; const filteredShapes = shapes.filter(shape => shape.options.tag === type); res.json(filteredShapes); });
app.listen(port, () => { console.log(Server running on http://localhost:${port}); }); файл index html:
#info { margin-top: 10px; }
.active { background-color: yellow; }
/* Скрыть атрибуцию / .leaflet-control-attribution { display: none; }
/ Добавленные стили для всплывающей подсказки */ .map-tooltip { background-color: white; border: 1px solid #ccc; padding: 5px; font-size: 12px; }
const drawnItems = new L.FeatureGroup().addTo(map); const drawControl = new L.Control.Draw({ draw: { polyline: false, polygon: true, circle: false, marker: false, rectangle: true }, edit: { featureGroup: drawnItems } }); map.addControl(drawControl);
let shapes = [];
map.on(‘draw:created’, function (e) { const layer = e.layer; drawnItems.addLayer(layer); const infoDiv = document.getElementById(‘info’); infoDiv.innerHTML = ‘Object info:’; const infoElement = document.createElement(‘p’);
if (layer.options.tag) { infoElement.textContent = Tag: {layer._leaflet_id}; } else { infoElement.textContent = ID: ${layer._leaflet_id}; }
- Assistant