Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get layer to top in Shiny Leaflet map

Tags:

r

leaflet

shiny

I have a map with different polygons on it which intersect at a lot of points and which can be on top of each other randomly due to some calculation and drawing processes. But there is one group of polygons which should be always on top, because they have popups attached to it.

Unfortunately anything like

leafletProxy("map") %>% showGroup("Layer1") %>% showGroup("Layer2")

or

leafletProxy("map") %>% hideGroup("Layer1") %>% hideGroup("Layer2") %>% 
showGroup("Layer1") %>% showGroup("Layer2")

does not change the order of the polygons.

Is there anything like bringToFront possible with leaflet package and shiny?

like image 495
SebBFE Avatar asked Oct 15 '15 08:10

SebBFE


1 Answers

Realizing this is now four years old, but I just came across this same problem and it can be solved with addMapPane. In the example above

leafletProxy("map") %>% addMapPane("layer1", zIndex=420) %>% addMapPane("layer2",zIndex=410)

That will keep layer1 above layer2 everywhere they intersect. Then it is just a question of assigning the layers to your polygons or whatever they are. For example:

leafletProxy("map") %>% addPolygons(data=layer1_data,options = pathOptions(pane = "layer1")) %>% addPolygons(data=layer2_data, options= options = pathOptions(pane = "layer2"))
like image 177
EOShaugh Avatar answered Sep 28 '22 16:09

EOShaugh