Skip to content

fill_layer

Stable

Fills polygons from a source.

The example's template and LiveView, read from the files that run in the preview.

lib/maplibrex_demo_web/examples/fill_layer_example.html.heex
<div id="fill-layer-example">
  <.example_frame error={@map_error} error_id="example-fill-map-error">
    <.map
      id="example-fill-map"
      center={[4.8952, 52.3702]}
      zoom={13}
      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-fill-osm"
    map_id="example-fill-map"
    url="https://tiles.openfreemap.org/planet"
    events={["source:error"]}
  />

  <.fill_layer
    id="example-fill-water"
    map_id="example-fill-map"
    source_id="example-fill-osm"
    source_layer="water"
    paint={paint(@color, @opacity, @outline, @theme)}
    events={["layer:feature_clicked"]}
  />

  <div class="space-y-3 border-t border-line px-4 py-4">
    <.example_group id="example-fill-assigns" label={gettext("Set assigns")}>
      <.example_button
        :for={{name, hex} <- colors()}
        id={"example-fill-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-fill-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[:opacity]}
          label="fill-opacity"
          min="0.1"
          max="1"
          step="0.05"
        />
        <.example_checkbox field={@options[:outline]} label="fill-outline-color" />
      </.form>
    </.example_group>

    <.example_note id="example-fill-data" icon="hero-cursor-arrow-rays-micro">
      {gettext("Amsterdam's water from OpenStreetMap, in OpenFreeMap's tiles. Click the water.")}
    </.example_note>
  </div>

  <%!-- layer:feature_clicked sets selected. --%>
  <.example_readout values={[
    color: @color,
    opacity: @opacity,
    outline: @outline,
    selected: @selected || "—"
  ]} />
</div>
lib/maplibrex_demo_web/examples/fill_layer_example.ex
defmodule MaplibrexDemoWeb.Examples.FillLayerExample do
  @moduledoc """
  The example of `<.fill_layer>`: the water of OpenStreetMap, from
  OpenFreeMap's vector tiles, filled over the basemap. Colour, opacity and
  outline are paint set from the assigns.
  """
  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"

  @colors [
    {"purple", "#7c5cd6"},
    {"teal", "#0f8b8d"},
    {"orange", "#d9480f"},
    {"red", "#c92a2a"}
  ]

  @impl true
  def mount(_params, _session, socket) do
    socket =
      socket
      |> assign(color: "#0f8b8d", selected: nil, map_error: nil)
      |> assign_options(0.6, true)

    {:ok, socket}
  end

  @impl true
  def handle_event("color", %{"color" => color}, socket) do
    if Enum.any?(@colors, fn {_name, hex} -> hex == color end) do
      {:noreply, assign(socket, :color, color)}
    else
      {:noreply, socket}
    end
  end

  def handle_event("options", %{"opacity" => opacity, "outline" => outline}, socket) do
    case Float.parse(opacity) do
      {opacity, _rest} -> {:noreply, assign_options(socket, opacity, outline == "true")}
      :error -> {:noreply, socket}
    end
  end

  # A click on the water carries its feature; OpenMapTiles names its kind
  # "class".
  def handle_event("layer:feature_clicked", %{"feature" => feature}, socket) do
    {:noreply, assign(socket, :selected, feature["properties"]["class"])}
  end

  def handle_event(event, %{"error" => message}, socket)
      when event in ["map:error", "source:error"] do
    {:noreply, assign(socket, :map_error, message)}
  end

  # Without an outline the property is left out, and MaplibreX sets it back
  # to MapLibre's default: the fill colour. The outline is dark on the light
  # basemap and light on the dark one.
  defp paint(color, opacity, outline, theme) do
    paint = %{"fill-color" => color, "fill-opacity" => opacity}
    if outline, do: Map.put(paint, "fill-outline-color", outline_color(theme)), else: paint
  end

  defp outline_color("dark"), do: "#e8e9f1"
  defp outline_color("light"), do: "#1f2330"

  defp assign_options(socket, opacity, outline) do
    assign(socket,
      opacity: opacity,
      outline: outline,
      options: to_form(%{"opacity" => opacity, "outline" => outline})
    )
  end

  defp colors, do: @colors
end