1 //= depend_on settings.yml
2 //= depend_on settings.local.yml
3 //= depend_on layers.yml
4 //= require leaflet/dist/leaflet-src
5 //= require leaflet.osm
9 if (navigator.languages) {
10 I18n.locale = navigator.languages[0];
11 } else if (navigator.language) {
12 I18n.locale = navigator.language;
15 I18n.default_locale = <%= I18n.default_locale.to_json %>;
16 I18n.fallbacks = true;
18 window.onload = function () {
19 const args = Object.fromEntries(new URLSearchParams(location.search));
23 <% if Settings.key?(:tile_cdn_url) %>
24 url: <%= Settings.tile_cdn_url.to_json %>
29 const map = L.map("map");
30 map.attributionControl.setPrefix("");
31 map.removeControl(map.attributionControl);
33 const isDarkTheme = args.theme === "dark" || (args.theme !== "light" && window.matchMedia("(prefers-color-scheme: dark)").matches);
34 const layers = <%= MapLayers::embed_definitions("config/layers.yml").to_json %>;
35 const layerId = (args.layer || "").replaceAll(" ", "");
36 const layerConfig = layers[layerId] || layers.mapnik;
37 const layer = (isDarkTheme && layerConfig.leafletOsmDarkId) || layerConfig.leafletOsmId;
38 new L.OSM[layer]({ apikey: layerConfig.apikey, ...options[layerId] }).addTo(map);
41 L.marker(args.marker.split(","), { icon: L.icon({
42 iconUrl: <%= asset_path('leaflet/dist/images/marker-icon.png').to_json %>,
43 iconSize: new L.Point(25, 41),
44 iconAnchor: new L.Point(12, 41),
45 shadowUrl: <%= asset_path('leaflet/dist/images/marker-shadow.png').to_json %>,
46 shadowSize: new L.Point(41, 41)
50 const bbox = (args.bbox || "-180,-90,180,90").split(",");
51 map.fitBounds([[bbox[1], bbox[0]], [bbox[3], bbox[2]]]);
53 map.addControl(new L.Control.OSMReportAProblem());
56 L.Control.OSMReportAProblem = L.Control.Attribution.extend({
58 position: "bottomright",
59 prefix: `<a href="https://www.openstreetmap.org/fixthemap?lat={x}&lon={y}&zoom={z}" target="_blank">${I18n.t("javascripts.embed.report_problem")}</a>`
62 onAdd: function (map) {
63 const container = L.Control.Attribution.prototype.onAdd.call(this, map);
65 map.on("moveend", this._update, this);
70 _update: function () {
71 L.Control.Attribution.prototype._update.call(this);
73 this._container.innerHTML =
74 this._container.innerHTML
75 .replace("{x}", this._map.getCenter().lat)
76 .replace("{y}", this._map.getCenter().lng)
77 .replace("{z}", this._map.getZoom());