I have to RasterLayer
-objects ( raster1
and raster2
) in R. And I want to add them both to a leaflet-map. I also wanted to have the option to show or hide them. So I thought I'd add them with their names to the addLayersControl
-option. But it doesn't work. It shows both layers at the same time and I can't hide them. So far my code looks like this. I think the problem is in how to add them to the addLayersControl
-function.
leaflet() %>%
addTiles() %>%
addRasterImage(raster1, opacity = 0.3) %>%
addRasterImage(raster2, opacity = 0.3) %>%
addLayersControl(
baseGroups = c("OSM (default)"),
overlayGroups = c("raster1", "raster2"),
options = layersControlOptions(collapsed = FALSE)
)
I guess that's kind of easy,but I can't think of any sollution.
From the addLayersControl
help :
overlayGroups
character vector where each element is the name of a group . The user can turn each overlay group on or off independently.
So you need groups. From the addRasterImage
help :
group
the name of the group this raster image should belong to (see the same parameter under addTiles)
Your code need to be something like this:
leaflet() %>%
addTiles() %>%
addRasterImage(raster1, opacity = 0.3, group = 'raster1') %>%
addRasterImage(raster2, opacity = 0.3, group = 'raster2') %>%
addLayersControl(
baseGroups = c("OSM (default)"),
overlayGroups = c("raster1", "raster2"),
options = layersControlOptions(collapsed = FALSE)
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.