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>
Generated from the component's definition in the library, so it always matches the version this demo runs.
| Attribute | Type | Default | Description |
|---|---|---|---|
| id required | string | — | Unique identifier for the control |
| map_id required | string | — | ID of the map to add the control to |
| position | string |
"bottom-left"
|
Control position: "top-left", "top-right", "bottom-left", "bottom-right". Defaults to "bottom-left". A new value moves the control to that corner
|
| max_width | integer |
100
|
Maximum width of the scale control in pixels. Defaults to 100. A new value applies to the scale at once
|
| unit | string |
"metric"
|
Unit of measurement: "imperial" (miles/feet), "metric" (kilometers/meters), or "nautical" (nautical miles). Defaults to "metric". A new value applies to the scale at once
|
| global attributes | global | — | Any other HTML attribute, passed on to the component's element. |