4

如何将图标添加到包 Leaflet R 中的图层控件?我用以下代码制作了图标:

rojos <- makeAwesomeIcon(icon='ion-waterdrop', library='ion', markerColor = 'red', iconColor = 'white')
verdes <- makeAwesomeIcon(icon='ion-waterdrop', library='ion', markerColor = 'green', iconColor = 'white')

并使用以下代码制作了地图

agua <-leaflet(options = leafletOptions(zoomControl = TRUE,
                                        minZoom = 10, maxZoom = 17,
                                        dragging = TRUE))%>%
  addTiles()%>% 
  setView(-101.145,19.793, 10) %>%
  # MAPAS BASE
  addProviderTiles(providers$OpenStreetMap.BlackAndWhite, group = "Calles") %>%
  addProviderTiles(providers$Esri.WorldImagery, group = "Imagen satelital") %>% 
  ###########################################################################################################
addGeoJSONv2(
  jsonlite::toJSON(rojo),
  markerType="marker",
  markerIcons = rojos,
  popupProperty='popup', 
  labelProperty='NOMBRE DEL CUERPO DE AGUA',
  group = "Agua contaminada") %>%
  addGeoJSONv2(
    jsonlite::toJSON(verde),
    markerType="marker",
    markerIcons = verdes,
    popupProperty='popup', 
    labelProperty='NOMBRE DEL CUERPO DE AGUA',
    group = "Agua no contaminada") %>%
  #POLIGONOS
  addPolygons(data = cuitzeo, col="green",fillColor="Transparent", group = "Cuenca de Cuitzeo",
              weight = 3, opacity = 1)%>%
  addPolygons(data = pol_mor, col="#000000",fillColor="Transparent",  
              group = "Límite Municipal Morelia",
              weight = 2, opacity = 1, fillOpacity = .8) %>%
  # CONTROL DE CAPAS
  addLayersControl(
    baseGroups = c("Calles","Imagen satelital"),
    overlayGroups = c("Agua contaminada","Agua no contaminada","Cuenca de Cuitzeo","Límite Municipal Morelia"),
    options = layersControlOptions(collapsed = F)
  )

结果如下:

在此处输入图像描述

我想得到这种风格的东西,但是用我的图标:

在此处输入图像描述

4

1 回答 1

2

有一种相对简单的方法可以做到这一点,它使用 r 中的传单的功能并且不依赖于自定义 javascript 控件:在组名中包含 html 标记。

而不是命名一个组:“组 A”,将其命名为:

<div style='position: relative; display: inline-block' class='awesome-marker-icon-blue awesome-marker'>
   <i class='glyphicon glyphicon-glass icon-black '></i>
</div>
Group A

例如,您可以基于图标名称以编程方式相当容易地创建此名称。每个名称只有几处变化:颜色、库(fa、ion、glyphicon)、图标类(例如:fa-glass、ion-checkmark、glyphicon-fire)和显示的组名。

这种方法创建了一个看起来像你想要的图层控件:

在此处输入图像描述

为了使事情更容易,将名称存储在命名列表/向量中,并在附加层时使用它来定义组,并在定义哪些组应该在控件中时再次使用它来定义组。这是一个基本示例:

library(leaflet)

IconSet <- awesomeIconList(
  "Cruise Ship"   = makeAwesomeIcon(icon= 'glass', markerColor = 'blue', iconColor = 'black', library = "glyphicon"),
  "Pirate Ship" = makeAwesomeIcon(icon= 'fire', markerColor = 'black', iconColor = 'white', library = "glyphicon")
)

# Some fake data
df <- sp::SpatialPointsDataFrame(
  cbind(
    (runif(20) - .5) * 10 - 90.620130,  # lng
    (runif(20) - .5) * 3.8 + 25.638077  # lat
  ),
  data.frame(type = factor(
    ifelse(runif(20) > 0.75, "Pirate Ship", "Cruise Ship"),
    c("Cruise Ship", "Pirate Ship")
  ))
)

# group names:
groups <- c("Cruise Ship" <- "<div style='position: relative; display: inline-block' class='awesome-marker-icon-blue awesome-marker'><i class='glyphicon glyphicon-glass icon-black '></i></div>Cruise Ship",
            "Pirate Ship" <- "<div style='position: relative; display: inline-block' class='awesome-marker-icon-black awesome-marker'><i class='glyphicon glyphicon-fire icon-white '></i></div>Pirate Ship")


leaflet(df) %>% addTiles() %>%
  addAwesomeMarkers(icon = ~IconSet[type], group=~groups[type]) %>% 
  addLayersControl(                                                                                                           
    overlayGroups = groups,
    options = layersControlOptions(collapsed = FALSE)
  )

同样,如果使用几个图层或动态图层,创建一个获取图标数据并生成相应名称以供以后使用的函数应该不会太难,而不是对上面的名称进行硬编码。

无论如何,这应该可以用作在控件中实现图标的一种方式。

于 2017-11-11T05:20:11.777 回答