MaplibreX (MaplibreX v0.4.0)

Copy Markdown View Source

MapLibre GL JS components for Phoenix LiveView.

MaplibreX exposes MapLibre GL JS as declarative LiveView components: the map follows your assigns, and map interactions arrive as ordinary handle_event/3 callbacks. See the README for installation and the full component list.

Quick start

defmodule MyAppWeb.MapLive do
  use MyAppWeb, :live_view
  import MaplibreX.Components

  def mount(_params, _session, socket) do
    {:ok, assign(socket, center: [-74.5, 40], zoom: 9)}
  end

  def render(assigns) do
    ~H"""
    <.map id="my-map" center={@center} zoom={@zoom} class="h-96 w-full" events={["map:moved"]} />
    <.navigation_control id="nav" map_id="my-map" position="top-left" />
    """
  end

  def handle_event("map:moved", %{"center" => center, "zoom" => zoom}, socket) do
    {:noreply, assign(socket, center: center, zoom: zoom)}
  end
end

Configuration

Every value is optional; these are the defaults:

config :maplibrex,
  default_style: "https://demotiles.maplibre.org/style.json",
  default_center: [0, 0],
  default_zoom: 10

A <.map> given no style, center or zoom, or nil, uses these. With default_center: nil and default_zoom: nil, such a map opens at the camera of its style instead, or at MapLibre's own default ([0, 0], zoom 0) when the style has none.

Events

Components push these to your LiveView, when asked to:

  • map:loaded - the map finished loading
  • map:moved - the map was panned, zoomed, rotated or pitched (debounced 150ms)
  • map:clicked - the map was clicked
  • map:zoom_changed - the zoom level changed
  • map:error - MapLibre reported an error
  • marker:clicked, marker:drag_start, marker:dragging, marker:drag_end
  • layer:feature_clicked, layer:feature_mouseenter, layer:feature_mouseleave

A component pushes only the events its events attribute names, such as events={["map:moved"]} above, and none without it, so a LiveView needs a handle_event/3 clause only for the events it asks for (#45). events={:all} pushes every event the component emits, as components did before 0.4.0; a LiveView that uses it needs a clause for each of them, or it crashes on the first one it has none for. A name the component does not push raises an ArgumentError when it renders. Each component's docs list the events it can push.

Every layer:* event names its layer with "layer_id" (the canonical key). The same value is also sent as "layerId", kept for compatibility.

Controlling the map

Map commands are Phoenix.LiveView.JS structs, so they execute on the client with no server round-trip:

alias MaplibreX.Components.Map

<button phx-click={Map.fly_to("my-map", [-74.5, 40], 12)}>Fly to NYC</button>
<button phx-click={Map.zoom_in("my-map")}>Zoom in</button>

See MaplibreX.Components.Map for the full command set.

The same commands can be pushed from the server (map:fly_to, map:jump_to, map:fit_bounds, map:set_style, map:set_bearing, map:set_pitch, map:reset_north, map:zoom_in, map:zoom_out). Every map reacts unless the payload carries the optional id of one map; the others then ignore it:

push_event(socket, "map:fly_to", %{id: "detail", center: [-3.7, 40.4], zoom: 12})

The animated ones, map:fly_to and map:fit_bounds, respect the visitor's prefers-reduced-motion setting, as fly_to/4 and fit_bounds/3 do, unless the payload carries essential: true.

Summary

Functions

Returns every configured value for :maplibrex.

Returns the configured default map center as [longitude, latitude].

Returns the configured default map style URL.

Returns the configured default zoom level.

Returns the installed MaplibreX version.

Functions

config()

@spec config() :: keyword()

Returns every configured value for :maplibrex.

default_center()

@spec default_center() :: [number()] | nil

Returns the configured default map center as [longitude, latitude].

Defaults to [0, 0].

default_style()

@spec default_style() :: String.t() | map()

Returns the configured default map style URL.

Defaults to https://demotiles.maplibre.org/style.json.

default_zoom()

@spec default_zoom() :: number() | nil

Returns the configured default zoom level.

Defaults to 10.

version()

@spec version() :: String.t()

Returns the installed MaplibreX version.