R leaflet is drawing base layers on top of overlay layers

前端 未结 1 983
鱼传尺愫
鱼传尺愫 2021-01-29 04:26

I\'m running into a problem with leaflet for R: When I change the base layer (polygons), the overlay layer gets pushed behind the base layer. From my reading of the leafletR he

相关标签:
1条回答
  • 2021-01-29 05:00

    The polygon layers should always have a higher z-index than tile layers. The layers control uses this terminology because the base groups are intended to be base (tile) layers, and the overlay groups are intended to be overlay (polygon) layers.

    You can work around this with a bit of JavaScript, see the onRender stage added to the pipe (you only have to do this once for a given map object):

    leaflet(data = counties(state = "UT")) %>% 
      addPolygons(color = "red", fillColor = "orange", group = "base1", fillOpacity = 1) %>% 
      addPolygons(color = "pink", fillColor = "purple", group = "base2", fillOpacity = 1) %>% 
      addPolygons(color = "yellow", fillColor = "green", group = "overlay", fillOpacity = 0.5) %>% 
      addLayersControl(baseGroups = c("base1", "base2"), options = layersControlOptions(collapsed = F), overlayGroups = "overlay") %>%
      htmlwidgets::onRender("
        function(el, x) {
          this.on('baselayerchange', function(e) {
            e.layer.bringToBack();
          })
        }
      ")
    
    0 讨论(0)
提交回复
热议问题