传单更改群集标记颜色

Leaflet change cluster marker color

我正在使用 ShinyProxy 部署 R ShinyApps。

我在 Leaflet Map 标记点上绘制并将它们聚类,因为我有多个(实际上很多)相同坐标的点。

每个点可以处于两种状态,所以我将颜色更改为红色或绿色。

我也想更改“群集标记”的颜色:

这里有一张(简单的虚拟)照片可以更好地解释我的意思:

我搜索了很多,但找不到解决问题的方法。

这是我的部分代码:

 getColor <- function(dfMap) {
            dati = lapply(dfMap$Id, function(x){ if(x %in% alarm$id){return("red")}else{ return("green")}})
            return(unlist(dati))
          }
          
          icons <- awesomeIcons(
            icon = 'ios-close',
            iconColor = 'black',
            library = 'ion',
            markerColor = getColor(dfMap)
          )
          
          map =  leaflet(dfMap,options = leafletOptions(worldCopyJump = T)) %>% addTiles() %>%
            addAwesomeMarkers(as.numeric(dfMap$lon), as.numeric(dfMap$lat), icon=icons, label=~as.character(dfMap$Id), clusterOptions = markerClusterOptions())
          
          output$alarmMap = renderLeaflet(map)

数据是一个 DataFrame 具有:

我还有另一个 DataFrame,它只包含应该是红色的元素。

非常感谢您的帮助。

一些想法:

包括功能:

改变簇的颜色:

想法是看集群中是否有任何红色point/marker,如果有则颜色应为红色,否则为绿色。

df %>%
  mutate(
    col = c(
      "green", "green", "red", "green", "green",
       "red", "green", "green"
    )
  ) %>%
  leaflet() %>%
  addTiles() %>%
  addCircleMarkers(
    options = markerOptions(col = ~col),
    color = ~col,
    clusterOptions = markerClusterOptions(
      iconCreateFunction=JS("function (cluster) {    
    var markers = cluster.getAllChildMarkers();
    var childCount = cluster.getChildCount();
    var p = 0; 
    for (i = 0; i < markers.length; i++) {
      if(markers[i].options.col === 'red'){
        p = 1;
        break;
      }
    }
    if(p === 0){
      c = 'rgba(0, 255, 0, 1);'
    } else {
      c = 'rgba(255, 0, 0, 1);'
    }
    return new L.DivIcon({ html: '<div style=\"background-color:'+c+'\"><span>' + childCount + '</span></div>', className: 'marker-cluster', iconSize: new L.Point(40, 40)});
  }")
    )
  )

数据:

structure(list(lon = c(15.5898481, 15.5874236, 15.5843765, 15.5676274, 
15.5830977, 15.5701817, 15.5850915, 15.5738857), lat = c(46.9551744, 
46.9545258, 46.9556816, 46.9625003, 46.9560813, 46.9601925, 46.9539635, 
46.9586439)), row.names = c(NA, -8L), class = c("tbl_df", "tbl", 
"data.frame"))