R小册子在覆盖层顶部绘制基础层

时间:2017-05-22 21:13:14

标签: r leaflet r-leaflet

我遇到了R的传单问题:当我更改基础层(多边形)时,覆盖层被推到基础层后面。从我对leafletR帮助文件的阅读中,叠加层应该始终具有比基础层更高的z索引,并且应该保持在最顶层。

示例在这里:http://rpubs.com/bvila/overdraw

require(acs)
require(tigris)
require(leaflet)
require(tidyverse)

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 = 1) %>% 
  addLayersControl(baseGroups = c("base1", "base2"), options = layersControlOptions(collapsed = F), overlayGroups = "overlay")

1 个答案:

答案 0 :(得分:1)

多边形图层的z-index应始终高于图块层。图层控件使用此术语,因为基本组旨在作为基础(图块)图层,并且图层组旨在作为叠加(多边形)图层。

您可以使用一些JavaScript解决此问题,请参阅添加到管道的onRender阶段(对于给定的地图对象,您只需执行一次):

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();
      })
    }
  ")