Skip to content

geojson_layer

Stable

Draws GeoJSON data as a fill, line, circle, symbol, heatmap or fill-extrusion layer, with optional clustering and feature events.

Set assigns

cursor

Run commands

Change the data

Send it as

Sample data: twelve points placed on a ring for this example. Click one.

@color
#7c5cd6
@radius
8
@visible
true
@cursor
pointer
@transition
1000
@send
data
@moved
nil
@selected
—
lib/maplibrex_demo_web/examples/geojson_layer_example.html.heex
<div id="geojson-layer-example">
  <.example_frame error={@map_error} error_id="example-geojson-map-error">
    <.map
      id="example-geojson-map"
      center={@center}
      zoom={10}
      style={basemap(@theme)}
      events={["map:error"]}
      class="h-full w-full"
    />
  </.example_frame>

  <%!-- The layer sits next to <.map>, not inside it, and names it with map_id.
        Every point has an id, so a point that moves goes to the map alone,
        and moves there over transition ms. --%>
  <.geojson_layer
    :if={@visible}
    id="example-geojson-points"
    map_id="example-geojson-map"
    type="circle"
    data={@points}
    paint={paint(@color, @radius)}
    transition={@transition}
    events={["layer:feature_clicked"]}
    cursor={@cursor}
  />

  <div class="space-y-3 border-t border-line px-4 py-4">
    <div class="grid gap-x-8 gap-y-4 lg:grid-cols-[1fr_auto]">
      <%!-- Assigns: each control changes an assign; the layer follows. --%>
      <.example_group id="example-geojson-assigns" label={gettext("Set assigns")}>
        <.example_button
          :for={{name, hex} <- colors()}
          id={"example-geojson-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-geojson-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[:radius]} label="radius" min="3" max="20" />
          <.example_checkbox field={@options[:visible]} label="visible" />
        </.form>

        <%!-- cursor: the CSS cursor over the points, which needs no event.
              This layer asks for one, and shows it instead of the pointer. --%>
        <.example_group id="example-geojson-cursor" label="cursor" code>
          <.example_button
            :for={cursor <- cursors()}
            id={"example-geojson-cursor-#{cursor}"}
            code
            pressed={@cursor == cursor}
            phx-click="cursor"
            phx-value-cursor={cursor}
          >
            {cursor}
          </.example_button>
        </.example_group>
      </.example_group>

      <%!-- Commands: JS structs that hide and show the layer in the browser,
            with no round trip, where :if above removes it and its source. --%>
      <.example_group id="example-geojson-commands" label={gettext("Run commands")}>
        <.example_button
          id="example-geojson-hide"
          code
          disabled={!@visible}
          phx-click={LayerCmd.hide("example-geojson-points")}
        >
          hide
        </.example_button>
        <.example_button
          id="example-geojson-show"
          code
          disabled={!@visible}
          phx-click={LayerCmd.show("example-geojson-points")}
        >
          show
        </.example_button>
      </.example_group>
    </div>

    <%!-- Data: the next point moves out to a wider ring, or back in, as new
          data (all twelve points go over the socket) or as a
          layer:update_data push (the moved point alone). --%>
    <div class="grid gap-x-8 gap-y-4 lg:grid-cols-[1fr_auto]">
      <.example_group id="example-geojson-data" label={gettext("Change the data")}>
        <.example_button id="example-geojson-move" phx-click="move" disabled={!@visible}>
          {gettext("Move the next point")}
        </.example_button>
        <.form for={@motion} id="example-geojson-motion" phx-change="motion" class="w-full pt-1">
          <.example_range
            field={@motion[:transition]}
            label="transition"
            min="0"
            max="3000"
            step="250"
          />
        </.form>
      </.example_group>

      <.example_group id="example-geojson-sends" label={gettext("Send it as")}>
        <.example_button
          :for={send <- sends()}
          id={"example-geojson-send-#{send}"}
          code
          pressed={@send == send}
          phx-click="send"
          phx-value-send={send}
        >
          {send_label(send)}
        </.example_button>
      </.example_group>
    </div>

    <.example_note id="example-geojson-sample-data">
      {gettext("Sample data: twelve points placed on a ring for this example. Click one.")}
    </.example_note>
  </div>

  <%!-- The assigns as they are now: layer:feature_clicked sets selected. --%>
  <.example_readout values={[
    color: @color,
    radius: @radius,
    visible: @visible,
    cursor: @cursor,
    transition: @transition,
    send: send_label(@send),
    moved: @moved || "nil",
    selected: @selected || "—"
  ]} />
</div>