scale_control
StableA scale bar for the current zoom level.
unit
position
Set assigns
- @unit
- metric
- @max_width
- 120
- @position
- bottom-left
- @zoom
- 11
<div id="scale-control-example">
<.example_frame error={@map_error} error_id="example-scale-map-error">
<.map
id="example-scale-map"
center={[-0.1276, 51.5072]}
zoom={@zoom}
style={basemap(@theme)}
events={["map:moved", "map:error"]}
class="h-full w-full"
/>
</.example_frame>
<%!-- The scale follows its assigns: a new unit or max_width applies to the
same bar, a new position moves it. --%>
<.scale_control
id="example-scale"
map_id="example-scale-map"
unit={@unit}
max_width={@max_width}
position={@position}
/>
<div class="space-y-4 border-t border-line px-4 py-4">
<div class="flex flex-wrap gap-x-8 gap-y-4">
<.example_group id="example-scale-units" label="unit" code>
<.example_button
:for={unit <- units()}
id={"example-scale-unit-#{unit}"}
code
pressed={@unit == unit}
phx-click="unit"
phx-value-unit={unit}
>
{unit}
</.example_button>
</.example_group>
<.example_group id="example-scale-positions" label="position" code>
<.example_button
:for={position <- positions()}
id={"example-scale-position-#{position}"}
code
pressed={@position == position}
phx-click="position"
phx-value-position={position}
>
{position}
</.example_button>
</.example_group>
</div>
<.example_group id="example-scale-assigns" label={gettext("Set assigns")}>
<.form
for={@options}
id="example-scale-options"
phx-change="options"
class="grid w-full gap-4 sm:max-w-2xl sm:grid-cols-2"
>
<.example_range
field={@options[:max_width]}
label="max_width"
min="60"
max="240"
step="20"
/>
<.example_range field={@options[:zoom]} label="zoom" min="2" max="18" step="0.5" />
</.form>
</.example_group>
</div>
<.example_readout values={[unit: @unit, max_width: @max_width, position: @position, zoom: @zoom]} />
</div>
The example's template and LiveView, read from the files that run in the preview.
<div id="scale-control-example">
<.example_frame error={@map_error} error_id="example-scale-map-error">
<.map
id="example-scale-map"
center={[-0.1276, 51.5072]}
zoom={@zoom}
style={basemap(@theme)}
events={["map:moved", "map:error"]}
class="h-full w-full"
/>
</.example_frame>
<%!-- The scale follows its assigns: a new unit or max_width applies to the
same bar, a new position moves it. --%>
<.scale_control
id="example-scale"
map_id="example-scale-map"
unit={@unit}
max_width={@max_width}
position={@position}
/>
<div class="space-y-4 border-t border-line px-4 py-4">
<div class="flex flex-wrap gap-x-8 gap-y-4">
<.example_group id="example-scale-units" label="unit" code>
<.example_button
:for={unit <- units()}
id={"example-scale-unit-#{unit}"}
code
pressed={@unit == unit}
phx-click="unit"
phx-value-unit={unit}
>
{unit}
</.example_button>
</.example_group>
<.example_group id="example-scale-positions" label="position" code>
<.example_button
:for={position <- positions()}
id={"example-scale-position-#{position}"}
code
pressed={@position == position}
phx-click="position"
phx-value-position={position}
>
{position}
</.example_button>
</.example_group>
</div>
<.example_group id="example-scale-assigns" label={gettext("Set assigns")}>
<.form
for={@options}
id="example-scale-options"
phx-change="options"
class="grid w-full gap-4 sm:max-w-2xl sm:grid-cols-2"
>
<.example_range
field={@options[:max_width]}
label="max_width"
min="60"
max="240"
step="20"
/>
<.example_range field={@options[:zoom]} label="zoom" min="2" max="18" step="0.5" />
</.form>
</.example_group>
</div>
<.example_readout values={[unit: @unit, max_width: @max_width, position: @position, zoom: @zoom]} />
</div>
defmodule MaplibrexDemoWeb.Examples.ScaleControlExample do
@moduledoc """
The example of `<.scale_control>`: a scale bar in the unit, width and
corner the assigns choose, following the map's zoom.
"""
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/liberty"
@units ~w(metric imperial nautical)
@positions ~w(top-left top-right bottom-left bottom-right)
@impl true
def mount(_params, _session, socket) do
socket =
socket
|> assign(unit: "metric", position: "bottom-left", map_error: nil)
|> assign(max_width: 120, zoom: 11)
|> assign_options()
{:ok, socket}
end
@impl true
def handle_event("unit", %{"unit" => unit}, socket) when unit in @units do
{:noreply, assign(socket, :unit, unit)}
end
def handle_event("position", %{"position" => position}, socket)
when position in @positions do
{:noreply, assign(socket, :position, position)}
end
def handle_event("options", %{"max_width" => max_width, "zoom" => zoom}, socket) do
{:noreply,
socket
|> assign(max_width: number(max_width), zoom: number(zoom))
|> assign_options()}
end
def handle_event(event, _params, socket) when event in ~w(unit position) do
{:noreply, socket}
end
# Zooming the map by hand changes the scale too; map:moved brings the zoom
# back so the slider stays in step.
def handle_event("map:moved", %{"zoom" => zoom}, socket) do
{:noreply, socket |> assign(:zoom, zoom) |> assign_options()}
end
def handle_event("map:error", %{"error" => message}, socket) do
{:noreply, assign(socket, :map_error, message)}
end
defp assign_options(socket) do
form = to_form(%{"max_width" => socket.assigns.max_width, "zoom" => socket.assigns.zoom})
assign(socket, :options, form)
end
defp units, do: @units
defp positions, do: @positions
# Slider values arrive as strings: "200", "6.5".
defp number(value) do
case Integer.parse(value) do
{integer, ""} -> integer
_ -> value |> Float.parse() |> elem(0)
end
end
end