【问题标题】:How can I programmatically create a list of objects of the leaflet icon class?如何以编程方式创建传单图标类的对象列表?
【发布时间】:2019-10-24 12:27:22
【问题描述】:

如何以编程方式创建传单图标类的对象列表?

我使用leaflet 库创建了一张地图,最终将显示几十个位置。为此,我想使用addMarkers 函数添加一些自定义图标,该函数采用使用iconList 创建的iconSet,如文档here 所述。

在下面的示例中(使用来自https://icon-library.net/ 的图标),通过iconList 创建myicons(其中包含对makeIcon 的两次直接调用)是没有问题的,因为只使用了两个图标。但是,在现实世界中,图标的数量、它们的 URL 和其他属性不会提前知道。

如果使用iconList 创建列表并使用cbind 将其作为新列附加到数据框,我会收到预期的“无法强制类”错误消息。

我唯一的选择似乎是以编程方式创建myicons 列表,但使用mynewicons <- iconList(sapply(1:nrow(df.data), function(i) {makeIcon(df.data$url[i],iconWidth = df.data$width[i],iconHeight = df.data$height[i])})) 之类的内容会导致Arguments passed to iconList() must be icon objects returned from makeIcon() 错误。

如何动态创建此传单图标列表而不是提前指定?

require(leaflet)
require(magrittr)

entrynames <- c("Entry 1","Entry 2")
lat <- c(51.509950,51.510736)
lng <- c(-0.1345093,-0.135190)
iconurl <- c("https://icon-library.net/images/right-arrow-icon-png/right-arrow-icon-png-9.jpg",
                "https://icon-library.net/images/back_previous_arrow_play_next_stop_pause_101040.png")
iconwidth <- c(60,50)
iconheight <- c(60,50)
df.data <- data.frame(entrynames=entrynames,lat=lat,lng=lng,
                      url=iconurl,width=iconwidth,height=iconheight,stringsAsFactors = FALSE)

df.data$entrynames <- as.character(df.data$entrynames)

myicons <- iconList(
    marker1 = makeIcon(iconUrl = df.data$url[1],iconWidth = df.data$width[1],iconHeight = df.data$height[1]),
    marker2 = makeIcon(iconUrl = df.data$url[2],iconWidth = df.data$width[2],iconHeight = df.data$height[2])
)

m <- leaflet() %>% setView(lng = -0.1345093, lat = 51.510090, zoom = 18) %>% addTiles() %>%
    addMarkers(data = df.data,
               lat = ~lat,
               lng = ~lng,
               icon = myicons)
m

MRE 输出:

【问题讨论】:

    标签: r r-leaflet


    【解决方案1】:

    这有点 hacky,我对传单不太熟悉,但使用 purrr::mappurrr::flatten 加上修复名称和属性似乎有效:

    require(leaflet)
    #> Indlæser krævet pakke: leaflet
    require(magrittr)
    #> Indlæser krævet pakke: magrittr
    
    entrynames <- c("Entry 1","Entry 2")
    lat <- c(51.509950,51.510736)
    lng <- c(-0.1345093,-0.135190)
    iconurl <- c("https://icon-library.net/images/right-arrow-icon-png/right-arrow-icon-png-9.jpg",
                 "https://icon-library.net/images/back_previous_arrow_play_next_stop_pause_101040.png")
    iconwidth <- c(60,50)
    iconheight <- c(60,50)
    df.data <- data.frame(entrynames=entrynames,lat=lat,lng=lng,
                          url=iconurl,width=iconwidth,height=iconheight,stringsAsFactors = FALSE)
    
    df.data$entrynames <- as.character(df.data$entrynames)
    
    myicons <- iconList(
      marker1 = makeIcon(iconUrl = df.data$url[1],iconWidth = df.data$width[1],iconHeight = df.data$height[1]),
      marker2 = makeIcon(iconUrl = df.data$url[2],iconWidth = df.data$width[2],iconHeight = df.data$height[2])
    )
    
    mynewicons <- purrr::map(1:nrow(df.data), 
                             function(i) {
                               iconList(makeIcon(df.data$url[i],
                                                 iconWidth = df.data$width[i],
                                                 iconHeight = df.data$height[i])
                                        )
                               }
                             ) %>% 
      purrr::flatten()
    names(mynewicons) <- glue::glue("marker{1:nrow(df.data)}")
    attr(mynewicons, "class") <-  "leaflet_icon_set"
    identical(myicons, mynewicons)
    #> [1] TRUE
    

    reprex package (v0.3.0) 于 2019 年 10 月 24 日创建

    【讨论】:

    • 如果它有效,我会接受 hacky - 并且看起来有效。谢谢。
    • purrr 映射的精彩使用
    猜你喜欢
    • 2016-10-10
    • 2016-11-03
    • 1970-01-01
    • 2012-07-27
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 2018-09-10
    • 2013-02-26
    相关资源
    最近更新 更多