HoloMap.Source (HoloMap v0.3.0)

Copy Markdown View Source

Namespace for the data sources a map can draw from.

A source says where the data comes from; a layer says how it looks. They are separate components because the relationship is one-to-many. One source routinely feeds a fill, an outline and a label layer, and re-declaring the data three times would mean fetching it three times.

<HoloMap.Source.GeoJSON id="parcels" data={@parcels} />
<HoloMap.Layer.Fill id="parcels-fill" source="parcels" paint={%{fill_color: "#3b82f6"}} />
<HoloMap.Layer.Line id="parcels-line" source="parcels" paint={%{line_color: "#1e40af"}} />

Layers reference a source by its id, and may be declared before or after it. A layer whose source is not on the map yet waits for it, and is added in its declared place as soon as the source is.

Available sources

Updating a source

How a source is updated depends on what changed. MapLibre changes some keys on the source itself, and no layer on it is disturbed:

  • A GeoJSON source's data goes through setData, which swaps the features and leaves the rest as it was.
  • A GeoJSON source's cluster, cluster_radius and cluster_max_zoom go through setClusterOptions, which builds the clusters again, before setData when data changed too.
  • A vector, raster or raster-dem source's tiles and url go through setTiles and setUrl, and MapLibre requests the tiles again, from the new URL or the TileJSON it names. The tiles on screen stay until the new ones arrive, and terrain drawn from the source stays on it.
  • An image source's url and coordinates go through updateImage.

Any other change is structural, and so is taking tiles, url, cluster_radius or cluster_max_zoom away, which MapLibre's setters cannot do. MapLibre refuses to remove a source that still has layers attached, so the reconciler detaches the layers it owns, swaps the source, and puts them back where they were in the stack, with the terrain.

Frequently-changing data therefore belongs in a GeoJSON source's data prop, and a changing tile set in tiles or url.