Skip to content

MaplibreX

MapLibre GL JS as declarative Phoenix LiveView components. The map is declared in HEEx and driven from the server: assigns change the map, map events arrive in handle_event/3, and the application writes no JavaScript.

This demo runs MaplibreX 0.8.0. It lists all 38 components with their status, and shows each working example next to the code that produces it.

Install

Requires Elixir 1.15 or later and Phoenix LiveView 1.0 or later. The steps start from an application generated by mix phx.new (Phoenix 1.8). For a path: dependency, deck.gl and the reasons behind each step, see the installation in the library's README.

  1. Add the dependency to mix.exs

    mix.exs
    def deps do
      [
        {:maplibrex, "~> 0.8.0"}
      ]
    end

    The demo itself runs MaplibreX 0.8.0, the latest release on Hex.

  2. Install MapLibre GL JS

    MaplibreX does not bundle MapLibre GL JS; your application owns its version. MaplibreX supports maplibre-gl 5 and 6; on version 6, deckgl_layer needs 6.9.1 or later.

    Shell
    npm install --prefix assets maplibre-gl
  3. Register the hooks

    In assets/js/app.js, add the hooks to the ones the generated LiveSocket already has:

    assets/js/app.js
    import { MapHooks } from "maplibrex"
    
    const liveSocket = new LiveSocket("/live", Socket, {
      longPollFallbackMs: 2500,
      params: {_csrf_token: csrfToken},
      hooks: {...colocatedHooks, ...MapHooks},
    })

    The bare "maplibrex" import resolves through NODE_PATH, which the generated esbuild profile points at deps/. The bundle found there imports maplibre-gl, which npm installed in assets/node_modules. In config/config.exs, add that directory to the NODE_PATH of the esbuild profile, after the two entries already there:

    config/config.exs
    env: %{
      "NODE_PATH" => [
        Path.expand("../deps", __DIR__),
        Mix.Project.build_path(),
        Path.expand("../assets/node_modules", __DIR__)
      ]
    }

    Without that entry the build stops with Could not resolve "maplibre-gl". If you did not install the deck.gl packages, which only deckgl_layer needs, also add --external:@deck.gl/* at the end of the profile's args.

  4. maplibre-gl 6 only: serve the worker

    maplibre-gl 6 starts its worker from separate files that esbuild does not copy into your build, and without them the map draws no tile data; maplibre-gl 5 needs none of this. MaplibreX's mix maplibrex.copy_worker copies them next to app.js: add it to the asset aliases in mix.exs, after esbuild:

    mix.exs
    "assets.build": ["compile", "tailwind my_app", "esbuild my_app", "maplibrex.copy_worker"],
    "assets.deploy": [
      "tailwind my_app --minify",
      "esbuild my_app --minify",
      "maplibrex.copy_worker",
      "phx.digest"
    ],

    Then point MapLibre at the copy, near the top of assets/js/app.js:

    assets/js/app.js
    import { getVersion, setWorkerUrl } from "maplibre-gl"
    
    setWorkerUrl(`/assets/js/maplibre-gl-worker.mjs?v=${getVersion()}`)

    Run mix assets.build after every install or upgrade of maplibre-gl: mix phx.server does not copy the files. With maplibre-gl 5 the task copies nothing and says so, so the aliases can keep it.

  5. Import the stylesheets

    assets/css/app.css
    @import "maplibre-gl/dist/maplibre-gl.css";
    @import "../../deps/maplibrex/assets/css/maplibrex.css";
  6. Import the components

    lib/my_app_web/live/map_live.ex
    defmodule MyAppWeb.MapLive do
      use MyAppWeb, :live_view
      import MaplibreX.Components
    
      # ...
    end

    A component pushes an event to your LiveView only when its events attribute names it, so a plain <.map> pushes none. Give each component the events you handle, such as events={["map:moved"]}, and write a handle_event/3 clause for each: an event with no matching clause crashes your LiveView.

A minimal map

A map, a navigation control and one marker per city. When the visitor pans or zooms, map:moved updates the assigns.

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

  <.marker :for={city <- @cities} id={city.id} map_id="map"
           lng_lat={city.coords} color="#22d3ee" popup_text={city.name} />
  """
end

# The map pushes only the events its `events` attribute names.
def handle_event("map:moved", %{"center" => center, "zoom" => zoom}, socket) do
  {:noreply, assign(socket, center: center, zoom: zoom)}
end

Components by family

The roadmap explains each status that is not stable, with its library issue.

Family Components
Map Stable 1 Limited 0 Planned 0 1
Markers & popups Stable 2 Limited 0 Planned 0 2
Layers Stable 11 Limited 0 Planned 0 11
Sources Stable 5 Limited 0 Planned 0 5
Images Stable 1 Limited 0 Planned 0 1
Controls Stable 12 Limited 0 Planned 0 12
3D Stable 2 Limited 0 Planned 0 2
Drawing Stable 1 Limited 0 Planned 0 1
Advanced Stable 3 Limited 0 Planned 0 3
All families Stable 38 Limited 0 Planned 0 38

Where to go next