Leaflet for R: How to change default CSS cluster classes

后端 未结 2 1293
梦如初夏
梦如初夏 2020-12-15 15:10

How do I change the default CSS classes which define cluster objects from within the Leaflet for R interface? For example, if I wanted to remove the opacity from the .marker

相关标签:
2条回答
  • One solution is to manually edit the default CSS file that comes with leaflet.R. I ran .libPaths() to find my library path, then did a grep search of the leaflet directory until I found Leaflet.markercluster here:

    /Library/Frameworks/R.framework/Versions/3.2/Resources/library/leaflet/htmlwidgets/plugins/Leaflet.markercluster

    from there, I opened the MarkerCluster.Defaults.css file and edited the CSS,

    .marker-cluster-small {
        background-color: rgba(181, 226, 140, 1.0);
        }
    .marker-cluster-small div {
        background-color: rgba(110, 204, 57, 1.0);
        }
    

    From here it would also be easy to define custom cluster classes. However, I'd like to avoid messing with the stability of the package if possible, and it would be great to be able to define the appropriate CSS on the fly form within leaflet.R or htmlwidgets.R

    0 讨论(0)
  • 2020-12-15 15:51

    You can maybe try to add inline CSS to the different markers in the function that creates the icons, for ex:

    clusterOptions = markerClusterOptions(iconCreateFunction=JS("function (cluster) {    
        var childCount = cluster.getChildCount();  
        if (childCount < 100) {  
          c = 'rgba(181, 226, 140, 1.0);'
        } else if (childCount < 1000) {  
          c = 'rgba(240, 194, 12, 1);'  
        } else { 
          c = 'rgba(241, 128, 23, 1);'  
        }    
        return new L.DivIcon({ html: '<div style=\"background-color:'+c+'\"><span>' + childCount + '</span></div>', className: 'marker-cluster', iconSize: new L.Point(40, 40) });
    
      }")
    

    If you are using shiny, you can also change the iconCreateFunction to assign a different class to each marker, and add tags$style in the header to set the CSS for these classes. Here's an example:

    ui <- fluidPage(
      tags$head(tags$style(HTML("
      .marker-custom-small {
      background-color: rgba(181, 226, 140, 1);
        }
    .marker-customr-small div {
        background-color: rgba(110, 204, 57, 1);
        }
    
    .marker-custom-medium {
        background-color: rgba(241, 211, 87, 1);
        }
    .marker-custom-medium div {
        background-color: rgba(240, 194, 12, 1);
        }
    
    .marker-custom-large {
        background-color: rgba(253, 156, 115, 1);
        }
    .marker-custom-large div {
        background-color: rgba(241, 128, 23, 1);
        }"))),
      leafletOutput("mymap"))
    
    server<-function(input, output, session) {
      output$mymap <- renderLeaflet({
        leaflet(quakes) %>% addTiles() %>% addMarkers(
          clusterOptions = markerClusterOptions(iconCreateFunction=JS("function (cluster) {    
        var childCount = cluster.getChildCount(); 
        var c = ' marker-custom-';  
        if (childCount < 100) {  
          c += 'large';  
        } else if (childCount < 1000) {  
          c += 'medium';  
        } else { 
          c += 'small';  
        }    
        return new L.DivIcon({ html: '<div><span>' + childCount + '</span></div>', className: 'marker-cluster' + c, iconSize: new L.Point(40, 40) });
    
      }"))
        )
      })
    }
    
    shinyApp(ui,server)
    

    Couldn't figure out how to have custom CSS in the leaflet outside of a shiny app.

    0 讨论(0)
提交回复
热议问题