Learn R Programming

mapgl (version 0.4.1)

add_control: Add a custom control to a map

Description

This function adds a custom control to a Mapbox GL or MapLibre GL map. It allows you to create custom HTML element controls and add them to the map.

Usage

add_control(
  map,
  html,
  position = "top-right",
  className = NULL,
  id = NULL,
  ...
)

Value

The modified map object with the custom control added.

Arguments

map

A map object created by the mapboxgl or maplibre functions.

html

Character string containing the HTML content for the control.

position

The position of the control. Can be one of "top-left", "top-right", "bottom-left", or "bottom-right". Default is "top-right".

className

Optional CSS class name for the control container.

id

Optional unique identifier for the control. If not provided, defaults to "custom". This ID can be used with clear_controls() to selectively remove this specific control.

...

Additional arguments passed to the JavaScript side.

Examples

Run this code
if (FALSE) {
library(mapgl)

# Basic custom control
maplibre() |>
  add_control(
    html = "
             Custom HTML
             
            ",
    position = "top-left"
  )

# Custom control with specific ID for selective removal
maplibre() |>
  add_control(
    html = "
             My Control
            ",
    position = "top-right",
    id = "my_custom_control"
  )

# Later, remove only this specific control
maplibre_proxy("map") |>
  clear_controls("my_custom_control")
}

Run the code above in your browser using DataLab