Skip to content

image

Stable

An image of your own on the map for symbol layers to draw: an icon from a URL, one icon in any colour with sdf, or a pulsing dot.

color

Vehicles in alert

Sample vehicles, not real ones: teal is moving, orange idle and grey stopped. Visitors who ask for less motion see the pulse still.

@color
#c92a2a
@period
1500
@alerts
TRK-3
lib/maplibrex_demo_web/examples/image_example.html.heex
<div id="image-example">
  <.example_frame error={@map_error} error_id="example-image-map-error">
    <.map
      id="example-image-map"
      center={[-0.3575, 39.4635]}
      zoom={12.6}
      style={basemap(@theme)}
      events={["map:error"]}
      class="h-full w-full"
    />
  </.example_frame>

  <%!-- Each <.image> puts an image on the map under its id, for layers to
        name in icon-image. The SVG takes a size; sdf makes it one shape
        that a layer paints with icon-color. --%>
  <.image
    id="example-image-vehicle"
    map_id="example-image-map"
    url="/images/vehicle.svg"
    width={24}
    height={24}
    sdf
    pixel_ratio={2}
  />

  <.image
    id="example-image-alert"
    map_id="example-image-map"
    animation={:pulse}
    color={@color}
    period={@period}
    size={64}
    pixel_ratio={2}
  />

  <.geojson_layer
    id="example-image-vehicles"
    map_id="example-image-map"
    type="symbol"
    data={fleet(@alerts)}
    layout={
      %{
        "icon-image" => "example-image-vehicle",
        "icon-rotate" => ["get", "heading"],
        "icon-rotation-alignment" => "map",
        "icon-allow-overlap" => true
      }
    }
    paint={
      %{"icon-color" => icon_color(), "icon-halo-color" => halo(@theme), "icon-halo-width" => 1.5}
    }
  />

  <%!-- The pulse under the vehicles in alert: before_id draws it below them. --%>
  <.geojson_layer
    id="example-image-alerts"
    map_id="example-image-map"
    type="symbol"
    data={fleet(@alerts)}
    filter={["get", "alert"]}
    before_id="example-image-vehicles"
    layout={%{"icon-image" => "example-image-alert", "icon-allow-overlap" => true}}
  />

  <div class="space-y-4 border-t border-line px-4 py-4">
    <div class="grid gap-x-8 gap-y-4 lg:grid-cols-[auto_1fr]">
      <.example_group id="example-image-colors" label="color" code>
        <.example_button
          :for={{name, hex} <- colors()}
          id={"example-image-color-#{name}"}
          code
          pressed={@color == hex}
          phx-click="pulse_color"
          phx-value-color={hex}
        >
          <.example_swatch color={hex} />{hex}
        </.example_button>
      </.example_group>

      <.example_group id="example-image-alert-toggles" label={gettext("Vehicles in alert")}>
        <.example_button
          :for={vehicle <- vehicles()}
          id={"example-image-alert-#{vehicle.id}"}
          code
          pressed={vehicle.id in @alerts}
          phx-click="alert"
          phx-value-id={vehicle.id}
        >
          {vehicle.id}
        </.example_button>
      </.example_group>
    </div>

    <.form
      for={@options}
      id="example-image-options"
      phx-change="options"
      class="w-full sm:max-w-xs"
    >
      <.example_range field={@options[:period]} label="period" min="500" max="3000" step="250" />
    </.form>

    <.example_note id="example-image-data">
      {gettext(
        "Sample vehicles, not real ones: teal is moving, orange idle and grey stopped. Visitors who ask for less motion see the pulse still."
      )}
    </.example_note>
  </div>

  <.example_readout values={[color: @color, period: @period, alerts: shown(@alerts)]} />
</div>