【问题标题】:Want to add 10 different types of icons to a leaflet map想要在传单地图中添加 10 种不同类型的图标
【发布时间】:2020-08-04 04:10:59
【问题描述】:

看看我的 R 代码,在这里我可以标记两种不同类型的标记图标,但我想用 10 种不同类型的标记图标标记 quakes$mag 列的所有 10 个值。

 quakes1 <- quakes[1:10,]

leafIcons <- icons(
  iconUrl = ifelse(quakes1$mag < 4.6,
                   "http://leafletjs.com/examples/custom-icons/leaf-green.png",
                   "http://leafletjs.com/examples/custom-icons/leaf-red.png"
  ),
  iconWidth = 38, iconHeight = 95,
  iconAnchorX = 22, iconAnchorY = 94,
  shadowUrl = "http://leafletjs.com/examples/custom-icons/leaf-shadow.png",
  shadowWidth = 50, shadowHeight = 64,
  shadowAnchorX = 4, shadowAnchorY = 62
)

leaflet(data = quakes1) %>% addTiles() %>%
  addMarkers(~long, ~lat, icon = leafIcons)

我尝试使用 switch 语句而不是 ifelse,但它不适用于 switch。

【问题讨论】:

    标签: r dictionary leaflet icons markers


    【解决方案1】:

    您的ifelse 是一个矢量化函数,它为leafIcons 创建了您的图标矢量。有多种方法可以矢量化 switch 或基于矢量创建 leafIcons 的替代方法 - 请参阅 this related question。使用dplyr,您可以使用case_when,这可能是您正在寻找的:

    library(dplyr)
    library(leaflet)
    
    leafIcons <- icons(
      iconUrl = case_when(
        quakes1$mag <= 4.3 ~ "http://leafletjs.com/examples/custom-icons/leaf-green.png",
        4.3 < quakes1$mag & quakes1$mag <= 5 ~ "http://leafletjs.com/examples/custom-icons/leaf-orange.png",
        quakes1$mag > 5 ~ "http://leafletjs.com/examples/custom-icons/leaf-red.png"
      ),
      iconWidth = 38, iconHeight = 95,
      iconAnchorX = 22, iconAnchorY = 94,
      shadowUrl = "http://leafletjs.com/examples/custom-icons/leaf-shadow.png",
      shadowWidth = 50, shadowHeight = 64,
      shadowAnchorX = 4, shadowAnchorY = 62
    )
    

    如果您想要将图标分配给不同范围的数字向量,也可以使用cut 或findInterval。

    【讨论】:

      猜你喜欢
      • 2022-07-28
      • 2016-10-14
      • 2019-08-20
      • 1970-01-01
      • 1970-01-01
      • 2018-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多