<% require 'uri' %> function getUrlParams() { const params = {}; const queryString = window.location.search.substring(1); const regex = /([^&=]+)=([^&]*)/g; let match; while (match = regex.exec(queryString)) { params[decodeURIComponent(match[1])] = decodeURIComponent(match[2]); } return params; } function createMap(divName) { // Get URL parameters const params = getUrlParams(); const lon = params.lon ? parseFloat(params.lon) : null; const lat = params.lat ? parseFloat(params.lat) : null; const zoom = params.zoom ? parseInt(params.zoom) : null; // Create a map var map = L.map(divName, { worldCopyJump: true }); // Set initial view if URL parameters are available, otherwise fitBounds if (lat !== null && lon !== null && zoom !== null) { map.setView([lat, lon], zoom); } else { map.fitBounds(<%= @bbox.to_json %>); } // Create a layer switcher var layers = L.control.layers(null, null, {collapsed:false}).addTo(map); // Add OpenStreetMap layer layers.addBaseLayer(L.tileLayer("https://tile.openstreetmap.org/{z}/{x}/{y}.png", { attribution: "© OpenStreetMap and contributors, under an open license", maxZoom: 19 }), "OpenStreetMap"); <% @layers.sort_by { |layer| layer[:name] }.each do |layer| -%> // Create <%= layer[:name] %> layer var <%= layer[:name].gsub("-", "_") %> = L.tileLayer(<%= layer[:url].to_json %>, { attribution: <%= layer[:attribution].to_json %>, maxZoom: <%= layer[:maxZoom].to_json %> }); // Add <%= layer[:name] %> to layer switcher <% if layer[:overlay] -%> layers.addOverlay(<%= layer[:name].gsub("-", "_") %>, <%= layer[:title].to_json %>); <% else %> layers.addBaseLayer(<%= layer[:name].gsub("-", "_") %>, <%= layer[:title].to_json %>); <% end -%> <% if layer[:default] -%> // Add <%= layer[:name] %> to map <%= layer[:name].gsub("-", "_") %>.addTo(map); <% end -%> <% end -%> // Add the permalink control map.addControl(new L.Control.Permalink()); var lc = L.control.locate({ position: 'topright' }).addTo(map); return map; }