attribution_control
StableShows the attribution of the map's sources.
position
Set assigns
- @enabled
- true
- @position
- bottom-right
- @compact
- false
- @custom_attribution
- nil
- @map_attribution
- true
<div id="attribution-control-example">
<.example_frame error={@map_error} error_id="example-attribution-map-error">
<.map
id="example-attribution-map"
center={[2.3522, 48.8566]}
zoom={12}
attribution_control={@map_attribution}
style={basemap(@theme)}
events={["map:error"]}
class="h-full w-full"
/>
</.example_frame>
<%!-- While it is on the page, the component takes the place of the map's
own attribution, so the map shows one. A new position moves it; a new
compact or custom_attribution replaces it in the same place. --%>
<.attribution_control
:if={@enabled}
id="example-attribution"
map_id="example-attribution-map"
position={@position}
compact={@compact}
custom_attribution={custom_attribution(@custom)}
/>
<div class="grid gap-x-8 gap-y-4 border-t border-line px-4 py-4 lg:grid-cols-[auto_1fr]">
<.example_group id="example-attribution-positions" label="position" code>
<.example_button
:for={position <- positions()}
id={"example-attribution-position-#{position}"}
code
pressed={@position == position}
phx-click="position"
phx-value-position={position}
>
{position}
</.example_button>
</.example_group>
<.example_group id="example-attribution-assigns" label={gettext("Set assigns")}>
<.form
for={@options}
id="example-attribution-options"
phx-change="options"
class="flex flex-wrap gap-x-6 gap-y-2 py-1.5"
>
<.example_checkbox field={@options[:enabled]} label=":if" />
<.example_checkbox field={@options[:compact]} label="compact" />
<.example_checkbox field={@options[:custom]} label="custom_attribution" />
<.example_checkbox field={@options[:map]} label="<.map attribution_control>" />
</.form>
</.example_group>
</div>
<.example_readout values={[
enabled: @enabled,
position: @position,
compact: @compact,
custom_attribution: custom_attribution(@custom) || "nil",
map_attribution: @map_attribution
]} />
</div>
The example's template and LiveView, read from the files that run in the preview.
<div id="attribution-control-example">
<.example_frame error={@map_error} error_id="example-attribution-map-error">
<.map
id="example-attribution-map"
center={[2.3522, 48.8566]}
zoom={12}
attribution_control={@map_attribution}
style={basemap(@theme)}
events={["map:error"]}
class="h-full w-full"
/>
</.example_frame>
<%!-- While it is on the page, the component takes the place of the map's
own attribution, so the map shows one. A new position moves it; a new
compact or custom_attribution replaces it in the same place. --%>
<.attribution_control
:if={@enabled}
id="example-attribution"
map_id="example-attribution-map"
position={@position}
compact={@compact}
custom_attribution={custom_attribution(@custom)}
/>
<div class="grid gap-x-8 gap-y-4 border-t border-line px-4 py-4 lg:grid-cols-[auto_1fr]">
<.example_group id="example-attribution-positions" label="position" code>
<.example_button
:for={position <- positions()}
id={"example-attribution-position-#{position}"}
code
pressed={@position == position}
phx-click="position"
phx-value-position={position}
>
{position}
</.example_button>
</.example_group>
<.example_group id="example-attribution-assigns" label={gettext("Set assigns")}>
<.form
for={@options}
id="example-attribution-options"
phx-change="options"
class="flex flex-wrap gap-x-6 gap-y-2 py-1.5"
>
<.example_checkbox field={@options[:enabled]} label=":if" />
<.example_checkbox field={@options[:compact]} label="compact" />
<.example_checkbox field={@options[:custom]} label="custom_attribution" />
<.example_checkbox field={@options[:map]} label="<.map attribution_control>" />
</.form>
</.example_group>
</div>
<.example_readout values={[
enabled: @enabled,
position: @position,
compact: @compact,
custom_attribution: custom_attribution(@custom) || "nil",
map_attribution: @map_attribution
]} />
</div>
defmodule MaplibrexDemoWeb.Examples.AttributionControlExample do
@moduledoc """
The example of `<.attribution_control>`: the map's attribution as a
component, in the corner, the compact form and with the extra text the
assigns choose. It takes the place of the map's own attribution while it
is on the page; removed with `:if`, the map shows its own again.
"""
use MaplibrexDemoWeb, :live_view
import MaplibreX.Components
import MaplibrexDemoWeb.Examples.ExampleComponents
# Demo plumbing, not needed in an application: applies the page's locale,
# copies each map event to the page's Events tab and keeps @theme on the
# page's light or dark theme.
on_mount MaplibrexDemoWeb.Examples.Plumbing
# OpenFreeMap's basemap for the page's theme: the map's style follows @theme.
defp basemap("dark"), do: "https://tiles.openfreemap.org/styles/dark"
defp basemap("light"), do: "https://tiles.openfreemap.org/styles/positron"
@positions ~w(top-left top-right bottom-left bottom-right)
# Shown after the sources' own attribution when `custom` is on.
@custom_attribution "MaplibreX demo"
@impl true
def mount(_params, _session, socket) do
socket =
socket
|> assign(position: "bottom-right", compact: false, custom: false, map_error: nil)
|> assign(enabled: true, map_attribution: true)
|> assign_options()
{:ok, socket}
end
@impl true
def handle_event("position", %{"position" => position}, socket)
when position in @positions do
{:noreply, assign(socket, :position, position)}
end
def handle_event("position", _params, socket), do: {:noreply, socket}
def handle_event("options", params, socket) do
{:noreply,
socket
|> assign(
enabled: params["enabled"] == "true",
compact: params["compact"] == "true",
custom: params["custom"] == "true",
map_attribution: params["map"] == "true"
)
|> assign_options()}
end
def handle_event("map:error", %{"error" => message}, socket) do
{:noreply, assign(socket, :map_error, message)}
end
defp assign_options(socket) do
%{enabled: enabled, compact: compact, custom: custom, map_attribution: map} = socket.assigns
form =
to_form(%{"enabled" => enabled, "compact" => compact, "custom" => custom, "map" => map})
assign(socket, :options, form)
end
defp custom_attribution(true), do: @custom_attribution
defp custom_attribution(false), do: nil
defp positions, do: @positions
end