Skip to contents

Add a source to the map

Usage

add_source(map, source_id, data, type = "geojson", cluster = FALSE, ...)

Arguments

map

The map or map proxy object.

source_id

The ID for the source.

data

The data for the source, typically in GeoJSON format.

type

The type of the source. Default is "geojson". Other options include "vector" or "raster".

cluster

Whether to enable clustering for this source. Default is FALSE.

...

Additional arguments to in pass directly to the JS addSource function. Documentation for this can be found on the MapLibre GL JS docs.

  • id: Alternative to source_id for backward compatibility. If both source_id and id are provided, source_id will take precedence.

Value

The map or map proxy object for chaining.

Note

If you add a source directly in an add layer function, the source ID will be automatically generated as source-{layer-id}.

Examples

# Load libraries
library(sf)

# Prepare data
data(quakes)
quakes_data <- sf::st_as_sf(quakes, coords = c("long", "lat"), crs = 4326)

# Display the source on map
map() |>
  add_source(
    source_id = "my_source",
    data = sf::st_as_sf(quakes_data, coords = c("long", "lat"), crs = 4326)
  ) |>
  add_circle_layer(id = "quakes", source = "my_source")