line_layer
StableDraws lines from a source, such as roads, routes or boundaries.
Set assigns
hit_width
Main roads from OpenStreetMap, in OpenFreeMap's tiles. Click one. With a hit_width, a click beside a road reaches it too: the pointer shows where.
- @color
- #7c5cd6
- @width
- 3
- @dashed
- false
- @hit_width
- nil
- @selected
- —
- @clicks
- 0
<div id="line-layer-example">
<.example_frame error={@map_error} error_id="example-line-map-error">
<.map
id="example-line-map"
center={[2.3522, 48.8566]}
zoom={11}
style={basemap(@theme)}
events={["map:error"]}
class="h-full w-full"
/>
</.example_frame>
<%!-- OpenFreeMap's TileJSON: OpenStreetMap in the OpenMapTiles schema. --%>
<.vector_tile_source
id="example-line-osm"
map_id="example-line-map"
url="https://tiles.openfreemap.org/planet"
events={["source:error"]}
/>
<%!-- The filter keeps the main roads of the "transportation" layer.
hit_width: the layer answers the pointer within that many pixels of
a road, for its click and its cursor; nil, exactly over the line. --%>
<.line_layer
id="example-line-roads"
map_id="example-line-map"
source_id="example-line-osm"
source_layer="transportation"
filter={["in", ["get", "class"], ["literal", ["motorway", "trunk", "primary"]]]}
layout={%{"line-cap" => "round", "line-join" => "round"}}
paint={paint(@color, @width, @dashed)}
events={["layer:feature_clicked"]}
cursor="pointer"
hit_width={@hit_width}
/>
<div class="space-y-3 border-t border-line px-4 py-4">
<.example_group id="example-line-assigns" label={gettext("Set assigns")}>
<.example_button
:for={{name, hex} <- colors()}
id={"example-line-color-#{name}"}
code
pressed={@color == hex}
phx-click="color"
phx-value-color={hex}
>
<.example_swatch color={hex} />{hex}
</.example_button>
<.form
for={@options}
id="example-line-options"
phx-change="options"
class="grid w-full gap-4 pt-2 sm:max-w-lg sm:grid-cols-[1fr_auto] sm:items-end"
>
<.example_range field={@options[:width]} label="line-width" min="1" max="10" />
<.example_checkbox field={@options[:dashed]} label="line-dasharray" />
</.form>
</.example_group>
<%!-- hit_width: how far from a road a click still reaches it. --%>
<.example_group id="example-line-hit-width" label="hit_width" code>
<.example_button
:for={hit_width <- hit_widths()}
id={"example-line-hit-width-#{hit_width_label(hit_width)}"}
code
pressed={@hit_width == hit_width}
phx-click="hit_width"
phx-value-width={hit_width_label(hit_width)}
>
{hit_width_label(hit_width)}
</.example_button>
</.example_group>
<.example_note id="example-line-data" icon="hero-cursor-arrow-rays-micro">
{gettext(
"Main roads from OpenStreetMap, in OpenFreeMap's tiles. Click one. With a hit_width, a click beside a road reaches it too: the pointer shows where."
)}
</.example_note>
</div>
<%!-- layer:feature_clicked sets selected and counts the click. --%>
<.example_readout values={[
color: @color,
width: @width,
dashed: @dashed,
hit_width: hit_width_label(@hit_width),
selected: @selected || "—",
clicks: @clicks
]} />
</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 layer |
| map_id required | string | — | ID of the map to attach to |
| source_id required | string | — | ID of the data source |
| source_layer | string |
nil
|
Layer name in the source (for vector tiles) |
| paint | map |
%{}
|
Paint properties (line-width, line-color, line-opacity, line-dasharray, etc.) |
| layout | map |
%{}
|
Layout properties (line-cap, line-join, visibility, etc.) |
| filter | any |
nil
|
Filter expression to show subset of features |
| min_zoom | integer |
nil
|
Minimum zoom level for layer visibility |
| max_zoom | integer |
nil
|
Maximum zoom level for layer visibility |
| before_id | string |
nil
|
ID of layer to insert this layer before |
| events | any |
[]
|
a list of event names, or :all
|
| cursor | string |
nil
|
a CSS cursor to show over the features, such as "pointer"
|
| hit_width | any |
nil
|
a number of pixels: the layer answers the pointer within that many pixels of what it draws |
| hover | boolean |
false
|
whether the feature under the pointer has the feature state hover, set in the browser
|
| feature_state | map |
nil
|
a map of feature id => map of state, which paint properties read with feature-state
|