heatmap_layer
StableShows the density of points from a source as a heatmap.
Points of interest in central Paris from OpenStreetMap, in OpenFreeMap's tiles: shops, cafés, museums and the like.
- @radius
- 10
- @intensity
- 0.3
- @on_map
- false
<div id="heatmap-layer-example">
<.example_frame error={@map_error} error_id="example-heatmap-map-error">
<.map
id="example-heatmap-map"
center={[2.3488, 48.8606]}
zoom={14}
style={basemap(@theme)}
events={["map:error"]}
class="h-full w-full"
/>
</.example_frame>
<%!-- OpenFreeMap's TileJSON: OpenStreetMap in the OpenMapTiles schema. --%>
<.vector_tile_source
id="example-heatmap-osm"
map_id="example-heatmap-map"
url="https://tiles.openfreemap.org/planet"
events={["source:error"]}
/>
<.heatmap_layer
id="example-heatmap-layer"
map_id="example-heatmap-map"
source_id="example-heatmap-osm"
source_layer="poi"
paint={paint(@radius, @intensity)}
events={["layer:added"]}
/>
<div class="space-y-3 border-t border-line px-4 py-4">
<.form
for={@options}
id="example-heatmap-options"
phx-change="options"
class="grid w-full gap-4 sm:max-w-lg sm:grid-cols-2"
>
<.example_range field={@options[:radius]} label="heatmap-radius" min="2" max="40" />
<.example_range
field={@options[:intensity]}
label="heatmap-intensity"
min="0.1"
max="2"
step="0.1"
/>
</.form>
<.example_note id="example-heatmap-data" icon="hero-map-micro">
{gettext(
"Points of interest in central Paris from OpenStreetMap, in OpenFreeMap's tiles: shops, cafés, museums and the like."
)}
</.example_note>
</div>
<%!-- layer:added sets on_map. --%>
<.example_readout values={[radius: @radius, intensity: @intensity, on_map: @on_map]} />
</div>
The example's template and LiveView, read from the files that run in the preview.
<div id="heatmap-layer-example">
<.example_frame error={@map_error} error_id="example-heatmap-map-error">
<.map
id="example-heatmap-map"
center={[2.3488, 48.8606]}
zoom={14}
style={basemap(@theme)}
events={["map:error"]}
class="h-full w-full"
/>
</.example_frame>
<%!-- OpenFreeMap's TileJSON: OpenStreetMap in the OpenMapTiles schema. --%>
<.vector_tile_source
id="example-heatmap-osm"
map_id="example-heatmap-map"
url="https://tiles.openfreemap.org/planet"
events={["source:error"]}
/>
<.heatmap_layer
id="example-heatmap-layer"
map_id="example-heatmap-map"
source_id="example-heatmap-osm"
source_layer="poi"
paint={paint(@radius, @intensity)}
events={["layer:added"]}
/>
<div class="space-y-3 border-t border-line px-4 py-4">
<.form
for={@options}
id="example-heatmap-options"
phx-change="options"
class="grid w-full gap-4 sm:max-w-lg sm:grid-cols-2"
>
<.example_range field={@options[:radius]} label="heatmap-radius" min="2" max="40" />
<.example_range
field={@options[:intensity]}
label="heatmap-intensity"
min="0.1"
max="2"
step="0.1"
/>
</.form>
<.example_note id="example-heatmap-data" icon="hero-map-micro">
{gettext(
"Points of interest in central Paris from OpenStreetMap, in OpenFreeMap's tiles: shops, cafés, museums and the like."
)}
</.example_note>
</div>
<%!-- layer:added sets on_map. --%>
<.example_readout values={[radius: @radius, intensity: @intensity, on_map: @on_map]} />
</div>
defmodule MaplibrexDemoWeb.Examples.HeatmapLayerExample do
@moduledoc """
The example of `<.heatmap_layer>`: where OpenStreetMap's points of
interest crowd together in central Paris, from OpenFreeMap's vector
tiles. The heatmap's radius and intensity are paint set from the assigns.
"""
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/positron"
@impl true
def mount(_params, _session, socket) do
socket =
socket
|> assign(on_map: false, map_error: nil)
|> assign_options(10, 0.3)
{:ok, socket}
end
@impl true
def handle_event("options", %{"radius" => radius, "intensity" => intensity}, socket) do
with {radius, _rest} <- Integer.parse(radius),
{intensity, _rest} <- Float.parse(intensity) do
{:noreply, assign_options(socket, radius, intensity)}
else
:error -> {:noreply, socket}
end
end
# A heatmap reports no clicks; layer:added says it is on the map.
def handle_event("layer:added", _payload, socket) do
{:noreply, assign(socket, :on_map, true)}
end
def handle_event(event, %{"error" => message}, socket)
when event in ["map:error", "source:error"] do
{:noreply, assign(socket, :map_error, message)}
end
# Density from transparent through purple to orange.
defp paint(radius, intensity) do
%{
"heatmap-radius" => radius,
"heatmap-intensity" => intensity,
"heatmap-opacity" => 0.8,
"heatmap-color" => [
"interpolate",
["linear"],
["heatmap-density"],
0,
"rgba(124, 92, 214, 0)",
0.3,
"rgba(124, 92, 214, 0.6)",
0.7,
"#c92a2a",
1,
"#f59f00"
]
}
end
defp assign_options(socket, radius, intensity) do
assign(socket,
radius: radius,
intensity: intensity,
options: to_form(%{"radius" => radius, "intensity" => intensity})
)
end
end