Loading...
Loading...
Use for web apps that need Leaflet-first GIS mapping, location selection, map-driven UIs, or geofencing validation. Covers Leaflet setup, optional tile providers, data storage, and backend validation patterns.
npx skill4agent add peterbamuhigire/skills-web-dev gis-mappingosm_api_keylatitudelongitudelocation_labeladdresslast_updated_at<link
rel="stylesheet"
href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css"
/>
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"></script>const map = L.map("map").setView([0.3476, 32.5825], 12);
const osmApiKey = window.osmApiKey || "";
const osmTileUrl = osmApiKey
? `https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png?api_key=${osmApiKey}`
: "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png";
const osm = L.tileLayer(osmTileUrl, {
attribution: "© OpenStreetMap contributors",
maxZoom: 19,
});
const terrain = L.tileLayer(
"https://{s}.tile.opentopomap.org/{z}/{x}/{y}.png",
{
attribution: "© OpenTopoMap",
maxZoom: 17,
},
);
terrain.addTo(map);let marker;
map.on("click", (e) => {
if (marker) map.removeLayer(marker);
marker = L.marker(e.latlng).addTo(map);
document.querySelector("#latitude").value = e.latlng.lat;
document.querySelector("#longitude").value = e.latlng.lng;
});const drawn = new L.FeatureGroup().addTo(map);
const drawControl = new L.Control.Draw({
draw: { polygon: true, rectangle: true, marker: false, circle: false },
edit: { featureGroup: drawn },
});
map.addControl(drawControl);
map.on("draw:created", (e) => {
drawn.clearLayers();
drawn.addLayer(e.layer);
document.querySelector("#boundary_geojson").value = JSON.stringify(
e.layer.toGeoJSON(),
);
});osm_api_key