Web Maps

Use the Leaflet map below to explore The National Map (TNM) base layers that describe the landscape of the United States and its territories. The base layers outside these areas may be unavailable at higher zoom levels.

map <- webmap::make_map(collapse = FALSE) |>
  leaflet::setView(lng = -98.583, lat = 39.833, zoom = 3)
map

Read a GeoJSON file containing the locations of U.S. cities.

city <- system.file("extdata/city.geojson", package = "webmap") |>
  sf::st_read(quiet = TRUE)
city
## Simple feature collection with 1005 features and 2 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: -157.8 ymin: 19.7 xmax: -69.77 ymax: 61.18
## Geodetic CRS:  WGS 84
## First 10 features:
##              name capital              geometry
## 1      Abilene TX       0  POINT (-99.74 32.45)
## 2        Akron OH       0  POINT (-81.52 41.08)
## 3      Alameda CA       0 POINT (-122.26 37.77)
## 4       Albany GA       0  POINT (-84.18 31.58)
## 5       Albany NY       2   POINT (-73.8 42.67)
## 6       Albany OR       0 POINT (-123.09 44.62)
## 7  Albuquerque NM       0 POINT (-106.62 35.12)
## 8   Alexandria LA       0  POINT (-92.46 31.29)
## 9   Alexandria VA       0  POINT (-77.09 38.82)
## 10    Alhambra CA       0 POINT (-118.13 34.08)

Create a new web map that includes only the "Topo" base map layer. And add clustered marker locations to call out city locations.

map <- webmap::make_map("Topo") |>
  leaflet::addMarkers(
    label = ~name,
    popup = ~name,
    clusterOptions = leaflet::markerClusterOptions(
      showCoverageOnHover = FALSE
    ),
    clusterId = "cluster",
    group = "marker",
    data = city
  )
map

Modify the existing map widget object (map) to place buttons on the map that adds control to (1) toggle full screen on and off, (2) zoom to the provided data extent, (3) toggle marker clusters on and off, and (4) search markers location by property.

map <- map |>
  webmap::add_fullscreen_button(pseudo_fullscreen = TRUE) |>
  webmap::add_home_button() |>
  webmap::add_cluster_button(cluster_id = "cluster") |>
  webmap::add_search_button(
    group = "marker",
    zoom = 15,
    text_placeholder = "Search city names..."
  )
map