开始使用免费开始使用

地图瓦片提供商

在上一个练习中,addTiles() 为您的 leaflet 地图添加了默认的 OpenStreetMap (OSM) 瓦片。地图瓦片会把多张地图图像拼接在一起。当用户缩放或平移地图时,这些瓦片会随之调整,从而实现您在练习 2 中体验到的交互功能。

leaflet 包内置了 100 多种可用的地图瓦片。这些瓦片保存在名为 providers 的列表中,您可以使用 addProviderTiles()(而不是 addTiles())将其添加到地图上。

已为您加载 leaflettidyverse 库。

本练习是课程的一部分

在 R 中使用 leaflet 构建交互式地图

查看课程

练习说明

  • 打印 leaflet 包中包含的 100 多种地图瓦片提供商列表。
  • 为了让输出更易读,仅使用 names() 函数打印这些提供商瓦片的名称。
  • 使用 stringr 包的 str_detect() 函数,找出哪些提供商瓦片名称包含字符串 "CartoDB"。
  • 打印每个名称中包含 "CartoDB" 的提供商地图瓦片名称。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Print the providers list included in the leaflet library
providers

# Print only the names of the map tiles in the providers list 
names(___)

# Use str_detect() to determine if the name of each provider tile contains the string "CartoDB"
str_detect(___(providers), "___")

# Use str_detect() to print only the provider tile names that include the string "CartoDB"
___(providers)[str_detect(names(providers), "___")]
编辑并运行代码