layer_switcher
StableA button in a corner of the map that opens a panel to choose the base map and turn overlays on and off.
Set assigns
Open the switcher in the map's top-right corner: choose a base map, and turn the overlays on and off. Your choice is kept in this browser for the next visit.
The page's theme switch gives the map a new style, as a LiveView may: that wins over the base chosen here, and the switcher shows the base of that style.
- @open
- false
- @base
- nil
- @overlay
- nil
<div id="layer-switcher-example">
<.example_frame error={@map_error} error_id="example-layer-switcher-map-error">
<.map
id="example-layer-switcher-map"
center={[4.8952, 52.3702]}
zoom={13}
style={basemap(@theme)}
events={["map:error"]}
class="h-full w-full"
/>
</.example_frame>
<%!-- OpenFreeMap's TileJSON: OpenStreetMap in the OpenMapTiles schema. The
layers below are the example's: a style change keeps them. --%>
<.vector_tile_source
id="example-layer-switcher-osm"
map_id="example-layer-switcher-map"
url="https://tiles.openfreemap.org/planet"
events={["source:error"]}
/>
<.fill_layer
id="example-layer-switcher-water"
map_id="example-layer-switcher-map"
source_id="example-layer-switcher-osm"
source_layer="water"
paint={%{"fill-color" => "#0f8b8d", "fill-opacity" => 0.5}}
/>
<.fill_layer
id="example-layer-switcher-buildings"
map_id="example-layer-switcher-map"
source_id="example-layer-switcher-osm"
source_layer="building"
paint={%{"fill-color" => "#d9480f", "fill-opacity" => 0.5}}
/>
<.line_layer
id="example-layer-switcher-roads"
map_id="example-layer-switcher-map"
source_id="example-layer-switcher-osm"
source_layer="transportation"
filter={["in", ["get", "class"], ["literal", ["motorway", "trunk", "primary"]]]}
paint={%{"line-color" => "#7c5cd6", "line-width" => 3}}
/>
<.line_layer
id="example-layer-switcher-rail"
map_id="example-layer-switcher-map"
source_id="example-layer-switcher-osm"
source_layer="transportation"
filter={["==", ["get", "class"], "rail"]}
paint={%{"line-color" => "#1971c2", "line-width" => 2}}
/>
<%!-- The switcher: bases, one at a time, and overlays that hide and show
their layers in the browser. Each entry has a key of its own, which
names it in the events and in what remember keeps, whatever the
language of its label. --%>
<.layer_switcher
id="example-layer-switcher"
map_id="example-layer-switcher-map"
label={gettext("Layers")}
base_label={gettext("Base map")}
open={@open}
remember
events={["layer_switcher:base_changed", "layer_switcher:overlay_toggled"]}
>
<:base :for={{key, label, style} <- bases()} key={key} label={label} style={style} />
<:overlay
key="roads"
label={gettext("Main roads")}
layers={["example-layer-switcher-roads"]}
group={gettext("Transport")}
/>
<:overlay
key="rail"
label={gettext("Railways")}
layers={["example-layer-switcher-rail"]}
group={gettext("Transport")}
/>
<:overlay key="water" label={gettext("Water")} layers={["example-layer-switcher-water"]} />
<:overlay
key="buildings"
label={gettext("Buildings")}
layers={["example-layer-switcher-buildings"]}
visible={false}
/>
</.layer_switcher>
<div class="space-y-3 border-t border-line px-4 py-4">
<.example_group id="example-layer-switcher-assigns" label={gettext("Set assigns")}>
<.form for={@options} id="example-layer-switcher-options" phx-change="options" class="pt-1">
<.example_checkbox field={@options[:open]} label="open" />
</.form>
</.example_group>
<.example_note id="example-layer-switcher-hint" icon="hero-cursor-arrow-rays-micro">
{gettext(
"Open the switcher in the map's top-right corner: choose a base map, and turn the overlays on and off. Your choice is kept in this browser for the next visit."
)}
</.example_note>
<.example_note id="example-layer-switcher-theme" icon="hero-information-circle-micro">
{gettext(
"The page's theme switch gives the map a new style, as a LiveView may: that wins over the base chosen here, and the switcher shows the base of that style."
)}
</.example_note>
</div>
<%!-- layer_switcher:base_changed sets base, and
layer_switcher:overlay_toggled sets overlay. --%>
<.example_readout values={[open: @open, base: @base || "nil", overlay: @overlay || "nil"]} />
</div>
The events this component sends to the LiveView, as the library documents them. Each arrives in handle_event/3. Use the preview; the last payload of each event the example receives appears here. A component sends only the events its events attribute names, so the example receives those its LiveView handles.
-
layer_switcher:base_changed
the user chose a base:
%{"switcher_id" => id, "key" => key}, with"style"too when the base'sstyleis a URL.Not received yet.
-
layer_switcher:overlay_toggled
the user turned an overlay on or off, by its checkbox or its group's:
%{"switcher_id" => id, "key" => key, "layers" => layers, "visible" => visible}, one for each overlay that changed.Not received yet.