navigation_control
StableZoom buttons and a compass.
<div id="navigation-control-example">
<.example_frame error={@map_error} error_id="example-navigation-map-error">
<.map
id="example-navigation-map"
center={[-43.2096, -22.9535]}
zoom={11}
pitch={45}
bearing={-30}
style={basemap(@theme)}
events={["map:error"]}
class="h-full w-full"
/>
</.example_frame>
<%!-- The control follows its assigns: a new position moves it, a new
option replaces it in the same place. Controls in one corner stand
in template order, so it stays above the full-screen button. --%>
<.navigation_control
id="example-navigation"
map_id="example-navigation-map"
position={@position}
show_compass={@show_compass}
show_zoom={@show_zoom}
visualize_pitch={@visualize_pitch}
/>
<.fullscreen_control id="example-navigation-fullscreen" map_id="example-navigation-map" />
<div class="grid gap-x-8 gap-y-4 border-t border-line px-4 py-4 lg:grid-cols-[auto_1fr]">
<.example_group id="example-navigation-positions" label="position" code>
<.example_button
:for={position <- positions()}
id={"example-navigation-position-#{position}"}
code
pressed={@position == position}
phx-click="position"
phx-value-position={position}
>
{position}
</.example_button>
</.example_group>
<.example_group id="example-navigation-assigns" label={gettext("Set assigns")}>
<.form
for={@options}
id="example-navigation-options"
phx-change="options"
class="flex flex-wrap gap-x-6 gap-y-2 py-1.5"
>
<.example_checkbox field={@options[:show_compass]} label="show_compass" />
<.example_checkbox field={@options[:show_zoom]} label="show_zoom" />
<.example_checkbox field={@options[:visualize_pitch]} label="visualize_pitch" />
</.form>
</.example_group>
</div>
<.example_readout values={[
position: @position,
show_compass: @show_compass,
show_zoom: @show_zoom,
visualize_pitch: @visualize_pitch
]} />
</div>
The example's template and LiveView, read from the files that run in the preview.
<div id="navigation-control-example">
<.example_frame error={@map_error} error_id="example-navigation-map-error">
<.map
id="example-navigation-map"
center={[-43.2096, -22.9535]}
zoom={11}
pitch={45}
bearing={-30}
style={basemap(@theme)}
events={["map:error"]}
class="h-full w-full"
/>
</.example_frame>
<%!-- The control follows its assigns: a new position moves it, a new
option replaces it in the same place. Controls in one corner stand
in template order, so it stays above the full-screen button. --%>
<.navigation_control
id="example-navigation"
map_id="example-navigation-map"
position={@position}
show_compass={@show_compass}
show_zoom={@show_zoom}
visualize_pitch={@visualize_pitch}
/>
<.fullscreen_control id="example-navigation-fullscreen" map_id="example-navigation-map" />
<div class="grid gap-x-8 gap-y-4 border-t border-line px-4 py-4 lg:grid-cols-[auto_1fr]">
<.example_group id="example-navigation-positions" label="position" code>
<.example_button
:for={position <- positions()}
id={"example-navigation-position-#{position}"}
code
pressed={@position == position}
phx-click="position"
phx-value-position={position}
>
{position}
</.example_button>
</.example_group>
<.example_group id="example-navigation-assigns" label={gettext("Set assigns")}>
<.form
for={@options}
id="example-navigation-options"
phx-change="options"
class="flex flex-wrap gap-x-6 gap-y-2 py-1.5"
>
<.example_checkbox field={@options[:show_compass]} label="show_compass" />
<.example_checkbox field={@options[:show_zoom]} label="show_zoom" />
<.example_checkbox field={@options[:visualize_pitch]} label="visualize_pitch" />
</.form>
</.example_group>
</div>
<.example_readout values={[
position: @position,
show_compass: @show_compass,
show_zoom: @show_zoom,
visualize_pitch: @visualize_pitch
]} />
</div>
defmodule MaplibrexDemoWeb.Examples.NavigationControlExample do
@moduledoc """
The example of `<.navigation_control>`: zoom buttons and a compass, in the
corner and with the parts the assigns choose, above a full-screen button
when they share a corner.
"""
use MaplibrexDemoWeb, :live_view
import MaplibreX.Components
import MaplibrexDemoWeb.Examples.ExampleComponents
# Demo plumbing, not needed in an application: applies the page's locale,
# copies each map event to the page's Events tab and keeps @theme on the
# page's light or dark theme.
on_mount MaplibrexDemoWeb.Examples.Plumbing
# OpenFreeMap's basemap for the page's theme: the map's style follows @theme.
defp basemap("dark"), do: "https://tiles.openfreemap.org/styles/dark"
defp basemap("light"), do: "https://tiles.openfreemap.org/styles/liberty"
@positions ~w(top-left top-right bottom-left bottom-right)
@impl true
def mount(_params, _session, socket) do
socket =
socket
|> assign(position: "top-right", map_error: nil)
|> assign(show_compass: true, show_zoom: true, visualize_pitch: true)
|> assign_options()
{:ok, socket}
end
@impl true
def handle_event("position", %{"position" => position}, socket)
when position in @positions do
{:noreply, assign(socket, :position, position)}
end
def handle_event("position", _params, socket), do: {:noreply, socket}
def handle_event("options", params, socket) do
{:noreply,
socket
|> assign(
show_compass: params["show_compass"] == "true",
show_zoom: params["show_zoom"] == "true",
visualize_pitch: params["visualize_pitch"] == "true"
)
|> assign_options()}
end
def handle_event("map:error", %{"error" => message}, socket) do
{:noreply, assign(socket, :map_error, message)}
end
defp assign_options(socket) do
form =
to_form(%{
"show_compass" => socket.assigns.show_compass,
"show_zoom" => socket.assigns.show_zoom,
"visualize_pitch" => socket.assigns.visualize_pitch
})
assign(socket, :options, form)
end
defp positions, do: @positions
end