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"}
/>
"""
end
def handle_event("layer:feature_clicked", %{"layerId" => id, "feature" => feature}, socket) do
IO.inspect(feature, label: "Clicked feature")
{:noreply, socket}
end
Summary
Functions
Renders a GeoJSON layer on the map.
Hides a layer by setting visibility to "none".
Updates the source data for a layer.
Sets a filter on a layer.
Sets a layout property on a layer.
Sets a paint property on a layer.
Shows a layer by setting visibility to "visible".
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 as a map or JSON stringtype(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 tofalse
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 when the source data is loaded
JavaScript Commands
You can control layers from LiveView using:
# 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)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.- Global attributes are accepted.
Hides a layer by setting visibility to "none".
Examples
<button phx-click={MaplibreX.Components.GeoJSONLayer.hide("layer-id")}>
Hide Layer
</button>
Updates the source data for a layer.
Examples
<button phx-click={MaplibreX.Components.GeoJSONLayer.set_data("layer-id", @new_data)}>
Update Data
</button>
Sets a filter on a layer.
Examples
<button phx-click={MaplibreX.Components.GeoJSONLayer.set_filter("layer-id", ["==", ["get", "type"], "park"])}>
Show Parks Only
</button>
Sets a layout property on a layer.
Examples
<button phx-click={MaplibreX.Components.GeoJSONLayer.set_layout_property("layer-id", "visibility", "none")}>
Hide Layer
</button>
Sets a paint property on a layer.
Examples
<button phx-click={MaplibreX.Components.GeoJSONLayer.set_paint_property("layer-id", "fill-color", "#ff0000")}>
Change Color
</button>
Shows a layer by setting visibility to "visible".
Examples
<button phx-click={MaplibreX.Components.GeoJSONLayer.show("layer-id")}>
Show Layer
</button>