GeoJSON Layer component for rendering GeoJSON data on MapLibre maps.
This component allows you to display GeoJSON data with full styling control and support for various layer types including fill, line, circle, symbol, and heatmap.
Examples
Basic GeoJSON fill layer:
<.geojson_layer
id="countries"
map_id="my-map"
data={@countries_geojson}
type="fill"
paint={%{
"fill-color" => "#088",
"fill-opacity" => 0.4
}}
/>Line layer with styling:
<.geojson_layer
id="routes"
map_id="my-map"
data={@routes_geojson}
type="line"
paint={%{
"line-color" => "#f00",
"line-width" => 3
}}
layout={%{
"line-cap" => "round",
"line-join" => "round"
}}
/>Circle layer with data-driven styling:
<.geojson_layer
id="cities"
map_id="my-map"
data={@cities_geojson}
type="circle"
paint={%{
"circle-radius" => [
"interpolate", ["linear"], ["get", "population"],
0, 4,
1000000, 20
],
"circle-color" => "#ff0000"
}}
/>Heatmap layer:
<.geojson_layer
id="earthquake-heat"
map_id="my-map"
data={@earthquakes_geojson}
type="heatmap"
paint={%{
"heatmap-weight" => ["get", "magnitude"],
"heatmap-intensity" => 1,
"heatmap-radius" => 20
}}
/>Layer with clustering:
<.geojson_layer
id="poi-clusters"
map_id="my-map"
data={@poi_geojson}
type="circle"
cluster={true}
cluster_max_zoom={14}
cluster_radius={50}
paint={%{
"circle-color" => "#51bbd6",
"circle-radius" => 10
}}
/>Layer with event handling:
def render(assigns) do
~H"""
<.geojson_layer
id="interactive-layer"
map_id="my-map"
data={@features}
type="fill"
paint={%{"fill-color" => "#088"}}
events={["layer:feature_clicked"]}
/>
"""
end
def handle_event("layer:feature_clicked", %{"layer_id" => id, "feature" => feature}, socket) do
IO.inspect(feature, label: "Clicked feature")
{:noreply, socket}
endData from a URL, which the browser fetches (see "Data" below):
<.geojson_layer
id="quakes"
map_id="my-map"
data="https://example.com/quakes.geojson"
type="circle"
/>Data
data takes the GeoJSON in one of three forms:
- a map, encoded to JSON when the component renders;
- JSON text, decoded when the component renders, so that invalid JSON raises there rather than in the browser;
- a URL: an
http://orhttps://URL, or a path from the root of your site, such as the one~p"/data/quakes.geojson"gives. The browser fetches it (MapLibre'ssetDatawith a URL), so the data does not go through the LiveView: from your own site, or from another one that allows your pages to fetch it with CORS headers.
A string that starts with http://, https:// or / is a URL, as no
JSON text can start that way; any other string is JSON text. A string
that is neither a URL of these kinds nor JSON raises an ArgumentError,
a relative path such as "data/quakes.geojson" included; JSON text that
starts with { and does not decode raises a Jason.DecodeError.
A new URL goes to the source as new data does, and so does a switch
between data and a URL, either way. layer:source_loaded comes once the
data has loaded; a fetch that fails is a MapLibre error, which the map
pushes as map:error when its events ask for it.
Summary
Functions
Renders a GeoJSON layer on the map.
Hides a layer by setting visibility to "none", in the browser.
Replaces the source data for a layer, in the browser.
Sets a filter on a layer, in the browser; nil removes it.
Sets a layout property on a layer, in the browser.
Sets a paint property on a layer, in the browser.
Shows a layer by setting visibility to "visible", in the browser.
Functions
Renders a GeoJSON layer on the map.
Attributes
id(required) - Unique identifier for the layermap_id(required) - ID of the map to add the layer todata(required) - GeoJSON data: a map, JSON text, or a URL the browser fetches (see "Data" in the module docs)type(required) - Layer type:"fill","line","circle","symbol","heatmap", or"fill-extrusion"paint- Paint properties for styling the layerlayout- Layout properties for the layerfilter- Filter expression to show only specific featuresmin_zoom- Minimum zoom level to show the layermax_zoom- Maximum zoom level to show the layerbefore_id- ID of layer to insert this layer beforesource_layer- For vector sources, the source layer to usecluster- Enable point clustering. Defaults tofalsecluster_max_zoom- Maximum zoom to cluster points. Defaults to14cluster_radius- Cluster radius in pixels. Defaults to50cluster_properties- Aggregate properties for clustersgenerate_id- Generate unique IDs for features. Defaults tofalsepromote_id- The name of a feature property that MapLibre takes each feature's id from, such as"uuid"; passed to the source aspromoteId. Feature state then addresses a feature by that property:map.setFeatureState({source: "<id>-source", id: feature.properties.uuid}, ...). It excludesgenerate_id, which numbers the features in order, so that their ids change with the data: giving both raises anArgumentError.events- The events to push to the LiveView: a list of names, such as["layer:feature_clicked"], or:allfor every one. Without it, the component pushes none, so the LiveView needs ahandle_event/3clause only for the events it lists. The names it takes are those under Events:layer:feature_clicked,layer:feature_mouseenter,layer:feature_mouseleave,layer:source_loaded; any other raises anArgumentError. A new value applies from the next event on
Slots
None - This component has no slots
Events
The layer emits the following events:
layer:feature_clicked- Fired when a feature is clickedlayer:feature_mouseenter- Fired when mouse enters a featurelayer:feature_mouseleave- Fired when mouse leaves a featurelayer:source_loaded- Fired once, withsourceId, the first time the layer's own source has loaded (not again on a map rendered again with the same id)
Every event identifies the layer with "layer_id", the same key the other
layer components use. "layerId" carries the same value and is kept for
compatibility with code written against 0.1.0.
Updates
The component owns a GeoJSON source, "<id>-source", and the layer drawing
it. Both follow every attribute. New data, turning cluster on, and new
cluster_radius or cluster_max_zoom values are applied to the source in
place; any other source change, a new generate_id or promote_id among
them, replaces the source under the layer. A
paint or layout property, or the filter, removed from the attributes goes
back to its default. The source and the layer come back after a map style
change.
The data and the other attributes update independently. The data is
rendered in an attribute of its own, data-geojson, apart from the others
in data-config, so LiveView sends it to the browser only when data
changes: a new paint value, from a slider say, is sent without the data
and leaves the source as it is, and new data goes to the source with
setData and changes nothing else.
JavaScript Commands
These functions return Phoenix.LiveView.JS commands that change the layer
in the browser, with no round trip to the server, like the map's
MaplibreX.Components.Map.fly_to/4. Use them in phx-click and the other
bindings:
# Update layer paint properties
MaplibreX.Components.GeoJSONLayer.set_paint_property("layer-id", "fill-color", "#ff0000")
# Update layer layout properties
MaplibreX.Components.GeoJSONLayer.set_layout_property("layer-id", "visibility", "none")
# Update layer filter
MaplibreX.Components.GeoJSONLayer.set_filter("layer-id", ["==", ["get", "type"], "park"])
# Update source data
MaplibreX.Components.GeoJSONLayer.set_data("layer-id", new_geojson_data)
# Show or hide the layer
MaplibreX.Components.GeoJSONLayer.hide("layer-id")Each one dispatches a DOM event to the layer's element, which its hook
handles: maplibrex:layer_set_paint_property,
maplibrex:layer_set_layout_property, maplibrex:layer_set_filter or
maplibrex:layer_set_data, with the payload of the server command of the
same name below as its detail. Your own JavaScript can dispatch them too.
From the server, push the same commands with Phoenix.LiveView.push_event/3:
layer:set_paint_property (layer_id, property, value),
layer:set_layout_property (the same), layer:set_filter (layer_id,
filter) and layer:set_data (layer_id, data).
What a command sets, from either side, is kept on top of the rendered
attributes, so it survives style changes and re-renders that leave that
property alone; a re-render that changes the same property, or a later
command, wins. A nil value resets the property to its default.
Attributes
id(:string) (required)map_id(:string) (required)data(:any) (required)type(:string) (required)paint(:map) - Defaults to%{}.layout(:map) - Defaults to%{}.filter(:any) - Defaults tonil.min_zoom(:integer) - Defaults tonil.max_zoom(:integer) - Defaults tonil.before_id(:string) - Defaults tonil.source_layer(:string) - Defaults tonil.cluster(:boolean) - Defaults tofalse.cluster_max_zoom(:integer) - Defaults to14.cluster_radius(:integer) - Defaults to50.cluster_properties(:map) - Defaults tonil.generate_id(:boolean) - Defaults tofalse.promote_id(:string) - Defaults tonil.events(:any) - a list of event names, or:all. Defaults to[].- Global attributes are accepted.
@spec hide(String.t()) :: Phoenix.LiveView.JS.t()
Hides a layer by setting visibility to "none", in the browser.
Examples
<button phx-click={MaplibreX.Components.GeoJSONLayer.hide("layer-id")}>
Hide Layer
</button>
@spec set_data(String.t(), map()) :: Phoenix.LiveView.JS.t()
Replaces the source data for a layer, in the browser.
The data goes into the page with the command (in the phx-click
attribute, say), so keep it small; for large or changing data, update
the data assign instead.
Examples
<button phx-click={MaplibreX.Components.GeoJSONLayer.set_data("layer-id", @new_data)}>
Update Data
</button>
@spec set_filter(String.t(), term()) :: Phoenix.LiveView.JS.t()
Sets a filter on a layer, in the browser; nil removes it.
Examples
<button phx-click={MaplibreX.Components.GeoJSONLayer.set_filter("layer-id", ["==", ["get", "type"], "park"])}>
Show Parks Only
</button>
@spec set_layout_property(String.t(), String.t(), term()) :: Phoenix.LiveView.JS.t()
Sets a layout property on a layer, in the browser.
Examples
<button phx-click={MaplibreX.Components.GeoJSONLayer.set_layout_property("layer-id", "visibility", "none")}>
Hide Layer
</button>
@spec set_paint_property(String.t(), String.t(), term()) :: Phoenix.LiveView.JS.t()
Sets a paint property on a layer, in the browser.
Examples
<button phx-click={MaplibreX.Components.GeoJSONLayer.set_paint_property("layer-id", "fill-color", "#ff0000")}>
Change Color
</button>
@spec show(String.t()) :: Phoenix.LiveView.JS.t()
Shows a layer by setting visibility to "visible", in the browser.
Examples
<button phx-click={MaplibreX.Components.GeoJSONLayer.show("layer-id")}>
Show Layer
</button>