geojson_layer
StableDraws GeoJSON data as a fill, line, circle, symbol, heatmap or fill-extrusion layer, with optional clustering and feature events.
Set assigns
cursor
Run commands
Change the data
Send it as
Sample data: twelve points placed on a ring for this example. Click one.
- @color
- #7c5cd6
- @radius
- 8
- @visible
- true
- @cursor
- pointer
- @transition
- 1000
- @send
- data
- @moved
- nil
- @selected
- —
<div id="geojson-layer-example">
<.example_frame error={@map_error} error_id="example-geojson-map-error">
<.map
id="example-geojson-map"
center={@center}
zoom={10}
style={basemap(@theme)}
events={["map:error"]}
class="h-full w-full"
/>
</.example_frame>
<%!-- The layer sits next to <.map>, not inside it, and names it with map_id.
Every point has an id, so a point that moves goes to the map alone,
and moves there over transition ms. --%>
<.geojson_layer
:if={@visible}
id="example-geojson-points"
map_id="example-geojson-map"
type="circle"
data={@points}
paint={paint(@color, @radius)}
transition={@transition}
events={["layer:feature_clicked"]}
cursor={@cursor}
/>
<div class="space-y-3 border-t border-line px-4 py-4">
<div class="grid gap-x-8 gap-y-4 lg:grid-cols-[1fr_auto]">
<%!-- Assigns: each control changes an assign; the layer follows. --%>
<.example_group id="example-geojson-assigns" label={gettext("Set assigns")}>
<.example_button
:for={{name, hex} <- colors()}
id={"example-geojson-color-#{name}"}
code
pressed={@color == hex}
phx-click="color"
phx-value-color={hex}
>
<.example_swatch color={hex} />{hex}
</.example_button>
<.form
for={@options}
id="example-geojson-options"
phx-change="options"
class="grid w-full gap-4 pt-2 sm:max-w-lg sm:grid-cols-[1fr_auto] sm:items-end"
>
<.example_range field={@options[:radius]} label="radius" min="3" max="20" />
<.example_checkbox field={@options[:visible]} label="visible" />
</.form>
<%!-- cursor: the CSS cursor over the points, which needs no event.
This layer asks for one, and shows it instead of the pointer. --%>
<.example_group id="example-geojson-cursor" label="cursor" code>
<.example_button
:for={cursor <- cursors()}
id={"example-geojson-cursor-#{cursor}"}
code
pressed={@cursor == cursor}
phx-click="cursor"
phx-value-cursor={cursor}
>
{cursor}
</.example_button>
</.example_group>
</.example_group>
<%!-- Commands: JS structs that hide and show the layer in the browser,
with no round trip, where :if above removes it and its source. --%>
<.example_group id="example-geojson-commands" label={gettext("Run commands")}>
<.example_button
id="example-geojson-hide"
code
disabled={!@visible}
phx-click={LayerCmd.hide("example-geojson-points")}
>
hide
</.example_button>
<.example_button
id="example-geojson-show"
code
disabled={!@visible}
phx-click={LayerCmd.show("example-geojson-points")}
>
show
</.example_button>
</.example_group>
</div>
<%!-- Data: the next point moves out to a wider ring, or back in, as new
data (all twelve points go over the socket) or as a
layer:update_data push (the moved point alone). --%>
<div class="grid gap-x-8 gap-y-4 lg:grid-cols-[1fr_auto]">
<.example_group id="example-geojson-data" label={gettext("Change the data")}>
<.example_button id="example-geojson-move" phx-click="move" disabled={!@visible}>
{gettext("Move the next point")}
</.example_button>
<.form for={@motion} id="example-geojson-motion" phx-change="motion" class="w-full pt-1">
<.example_range
field={@motion[:transition]}
label="transition"
min="0"
max="3000"
step="250"
/>
</.form>
</.example_group>
<.example_group id="example-geojson-sends" label={gettext("Send it as")}>
<.example_button
:for={send <- sends()}
id={"example-geojson-send-#{send}"}
code
pressed={@send == send}
phx-click="send"
phx-value-send={send}
>
{send_label(send)}
</.example_button>
</.example_group>
</div>
<.example_note id="example-geojson-sample-data">
{gettext("Sample data: twelve points placed on a ring for this example. Click one.")}
</.example_note>
</div>
<%!-- The assigns as they are now: layer:feature_clicked sets selected. --%>
<.example_readout values={[
color: @color,
radius: @radius,
visible: @visible,
cursor: @cursor,
transition: @transition,
send: send_label(@send),
moved: @moved || "nil",
selected: @selected || "—"
]} />
</div>
Generated from the component's definition in the library, so it always matches the version this demo runs.
| Attribute | Type | Default | Description |
|---|---|---|---|
| id required | string | — | Unique identifier for the layer |
| map_id required | string | — | ID of the map to add the layer to |
| data required | any | — | GeoJSON data: a map, JSON text, or a URL the browser fetches (see "Data" in the module docs). With an id on every feature, only what changed goes to the map (see "Updating part of the data") |
| type required | string | — |
Layer type: "fill", "line", "circle", "symbol", "heatmap", or "fill-extrusion"
|
| paint | map |
%{}
|
Paint properties for styling the layer |
| layout | map |
%{}
|
Layout properties for the layer |
| filter | any |
nil
|
Filter expression to show only specific features |
| min_zoom | integer |
nil
|
Minimum zoom level to show the layer |
| max_zoom | integer |
nil
|
Maximum zoom level to show the layer |
| before_id | string |
nil
|
ID of layer to insert this layer before |
| source_layer | string |
nil
|
For vector sources, the source layer to use |
| cluster | boolean |
false
|
Enable point clustering. Defaults to false
|
| cluster_max_zoom | integer |
14
|
Maximum zoom to cluster points. Defaults to 14
|
| cluster_radius | integer |
50
|
Cluster radius in pixels. Defaults to 50
|
| cluster_properties | map |
nil
|
Aggregate properties for clusters |
| generate_id | boolean |
false
|
Generate unique IDs for features. Defaults to false
|
| promote_id | string |
nil
|
The name of a feature property that MapLibre takes each feature's id from, such as "uuid"; passed to the source as promoteId. Feature state then addresses a feature by that property, in feature_state and from your own JavaScript: map.setFeatureState({source: "<id>-source", id: feature.properties.uuid}, ...). It excludes generate_id, which numbers the features in order, so that their ids change with the data: giving both raises an ArgumentError. The ids it gives also let the layer give the map only what changed in new data (see "Updating part of the data").
|
| global attributes | global | — | Any other HTML attribute, passed on to the component's element. |
| events | any |
[]
|
a list of event names, or :all
|
| cursor | string |
nil
|
a CSS cursor to show over the features, such as "pointer"
|
| transition | integer |
nil
|
how long a point feature with an id takes to move to a new position, in milliseconds; nil or 0 jumps |
| transition_heading | string |
nil
|
the name of the property holding a point's heading, which turns the short way during a move |
| transition_max_distance | any |
nil
|
the longest move, in metres on the ground: a longer one jumps; nil for no limit |
| feature_state | map |
nil
|
a map of feature id => map of state, which paint properties read with feature-state
|
| hit_width | any |
nil
|
a number of pixels: the layer answers the pointer within that many pixels of what it draws |
| hover | boolean |
false
|
whether the feature under the pointer has the feature state hover, set in the browser
|
| cluster_expand_on_click | boolean |
false
|
whether a click on a cluster moves the camera to where it breaks apart, in the browser |
| label_points | boolean |
false
|
whether the layer keeps a source of points, "<id>-labels", one inside each polygon, for labels
|
Slots
- cluster_marker HTML drawn for each cluster, a template filled in the browser from the cluster's properties; needs `cluster`