Map component for rendering MapLibre GL JS maps in Phoenix LiveView.
Examples
Basic map:
<.map
id="my-map"
center={[-74.5, 40]}
zoom={9}
style="https://demotiles.maplibre.org/style.json"
class="h-96"
/>Map with all options:
<.map
id="detailed-map"
center={[-74.5, 40]}
zoom={9}
style="https://demotiles.maplibre.org/style.json"
min_zoom={5}
max_zoom={15}
bearing={0}
pitch={0}
interactive={true}
attribution_control={true}
class="h-screen w-full"
/>Map with event handling:
def render(assigns) do
~H"""
<.map
id="interactive-map"
center={@center}
zoom={@zoom}
style={@map_style}
phx-click="map_clicked"
class="h-96"
/>
"""
end
def handle_event("map:clicked", %{"lngLat" => lngLat}, socket) do
IO.inspect(lngLat, label: "Map clicked at")
{:noreply, socket}
end
def handle_event("map:moved", %{"center" => center, "zoom" => zoom}, socket) do
{:noreply, assign(socket, center: center, zoom: zoom)}
end
Summary
Functions
Creates a JS command to fit the map to specific bounds.
Creates a JS command to fly the map to a specific location.
Creates a JS command to jump the map to a specific location (no animation).
Renders a MapLibre GL JS map component.
Creates a JS command to reset the map's bearing to north.
Creates a JS command to set the map style.
Creates a JS command to zoom in.
Creates a JS command to zoom out.
Types
Functions
@spec fit_bounds(String.t(), bounds(), keyword()) :: Phoenix.LiveView.JS.t()
Creates a JS command to fit the map to specific bounds.
Examples
bounds = [[-74, 40], [-73, 41]]
<button phx-click={MaplibreX.Components.Map.fit_bounds("my-map", bounds)}>
Fit to bounds
</button>With padding and max zoom:
fit_bounds("my-map", bounds, padding: 50, max_zoom: 15)
@spec fly_to(String.t(), center(), integer(), keyword()) :: Phoenix.LiveView.JS.t()
Creates a JS command to fly the map to a specific location.
Examples
<button phx-click={MaplibreX.Components.Map.fly_to("my-map", [-74.5, 40], 12)}>
Fly to NYC
</button>With custom duration:
fly_to("my-map", [-74.5, 40], 12, duration: 3000)
@spec jump_to(String.t(), center(), integer(), keyword()) :: Phoenix.LiveView.JS.t()
Creates a JS command to jump the map to a specific location (no animation).
Examples
<button phx-click={MaplibreX.Components.Map.jump_to("my-map", [-74.5, 40], 12)}>
Jump to NYC
</button>
Renders a MapLibre GL JS map component.
Attributes
id(required) - Unique identifier for the mapcenter- Map center coordinates as[longitude, latitude]. Defaults to[0, 0]zoom- Initial zoom level, fractional values allowed. Defaults to10style- Map style URL or style object. Defaults to MaplibreX configmin_zoom- Minimum zoom level. Optionalmax_zoom- Maximum zoom level. Optionalmin_pitch- Minimum pitch in degrees. Optionalmax_pitch- Maximum pitch in degrees, 0-85. Optional. MapLibre caps pitch at 60 by default and silently clamps anything above it, so a 3D terrain view that wants a steeper camera has to raise thisbearing- Initial bearing (rotation). Defaults to0pitch- Initial pitch (tilt). Defaults to0bounds- Fit map to bounds[[west, south], [east, north]]. Optionalmax_bounds- Maximum bounds the map can be panned to. Optionalinteractive- Whether the map is interactive. Defaults totrueattribution_control- Show attribution control. Defaults totrueclass- CSS classes for the map containertestid- Test ID for the map element
Events
The map emits the following events to LiveView:
map:moved- Fired when the map is moved (pan, zoom, rotate, pitch)map:clicked- Fired when the map is clickedmap:loaded- Fired when the map finishes loadingmap:zoom_changed- Fired when zoom level changesmap:error- Fired when an error occurs
Slots
inner_block- Optional content to render inside the map container
Attributes
id(:string) (required)center(:list) - Defaults tonil.zoom(:any) - number. Defaults tonil.style(:any) - Defaults tonil.min_zoom(:any) - number. Defaults tonil.max_zoom(:any) - number. Defaults tonil.min_pitch(:any) - number, degrees. Defaults tonil.max_pitch(:any) - number, degrees 0-85. Defaults tonil.bearing(:any) - number, degrees. Defaults to0.pitch(:any) - number, degrees. Defaults to0.bounds(:list) - Defaults tonil.max_bounds(:list) - Defaults tonil.interactive(:boolean) - Defaults totrue.attribution_control(:boolean) - Defaults totrue.class(:string) - Defaults to"maplibrex-map".testid(:string) - Defaults tonil.- Global attributes are accepted.
Slots
inner_block
@spec reset_north(String.t()) :: Phoenix.LiveView.JS.t()
Creates a JS command to reset the map's bearing to north.
@spec set_style(String.t(), String.t() | map()) :: Phoenix.LiveView.JS.t()
Creates a JS command to set the map style.
Examples
<button phx-click={MaplibreX.Components.Map.set_style("my-map", "new-style-url")}>
Change Style
</button>
@spec zoom_in(String.t()) :: Phoenix.LiveView.JS.t()
Creates a JS command to zoom in.
@spec zoom_out(String.t()) :: Phoenix.LiveView.JS.t()
Creates a JS command to zoom out.