Skip to content

line_layer

Stable

Draws 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
lib/maplibrex_demo_web/examples/line_layer_example.html.heex
<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>