2 function getUrlParams() {
4 const queryString = window.location.search.substring(1);
5 const regex = /([^&=]+)=([^&]*)/g;
8 while (match = regex.exec(queryString)) {
9 params[decodeURIComponent(match[1])] = decodeURIComponent(match[2]);
15 function createMap(divName) {
17 const params = getUrlParams();
18 const lon = params.lon ? parseFloat(params.lon) : null;
19 const lat = params.lat ? parseFloat(params.lat) : null;
20 const zoom = params.zoom ? parseInt(params.zoom) : null;
23 var map = L.map(divName, {
27 // Set initial view if URL parameters are available, otherwise fitBounds
28 if (lat !== null && lon !== null && zoom !== null) {
29 map.setView([lat, lon], zoom);
31 map.fitBounds(<%= @bbox.to_json %>);
34 // Create a layer switcher
35 var layers = L.control.layers(null, null, {collapsed:false}).addTo(map);
37 // Add OpenStreetMap layer
38 layers.addBaseLayer(L.tileLayer("https://tile.openstreetmap.org/{z}/{x}/{y}.png", {
39 attribution: "© <a target=\"_parent\" href=\"https://www.openstreetmap.org\">OpenStreetMap</a> and contributors, under an <a target=\"_parent\" href=\"https://www.openstreetmap.org/copyright\">open license</a>",
42 <% @layers.sort_by { |layer| layer[:name] }.each do |layer| -%>
44 // Create <%= layer[:name] %> layer
45 var <%= layer[:name].gsub("-", "_") %> = L.tileLayer(<%= layer[:url].to_json %>, {
46 attribution: <%= layer[:attribution].to_json %>,
47 maxZoom: <%= layer[:maxZoom].to_json %>
50 // Add <%= layer[:name] %> to layer switcher
51 <% if layer[:overlay] -%>
52 layers.addOverlay(<%= layer[:name].gsub("-", "_") %>, <%= layer[:title].to_json %>);
54 layers.addBaseLayer(<%= layer[:name].gsub("-", "_") %>, <%= layer[:title].to_json %>);
57 <% if layer[:default] -%>
58 // Add <%= layer[:name] %> to map
59 <%= layer[:name].gsub("-", "_") %>.addTo(map);
63 // Add the permalink control
64 map.addControl(new L.Control.Permalink());
66 var lc = L.control.locate({