r-spatial / leafgl

R package for fast web gl rendering for leaflet
Other
263 stars 31 forks source link

Is it possible to have more than one pop-up in the same map? #92

Open bmaparicio opened 6 months ago

bmaparicio commented 6 months ago

Hi, I have a map with several layers. I want to display a pop-up with the data of the layer that is on, but I can't do it. The only pop-up that shows is from the last layer created (it overwrites the other). In the figure below, obj_1 does not show any pop-up when clicking, but obj_3 does (the last layer created).

image

Is there a way to have one pop-up per layer? Thanks!

tim-salabim commented 6 months ago

Can we get a reproducible example showing the issue?

bmaparicio commented 6 months ago

Yes. Here is an open dataset:

library(leaflet) library(leafgl) library(sf) library(spData)

data("hawaii")

my_data<-sf::st_cast(hawaii,"POLYGON")%>% st_transform(crs = 4326)

set.seed(42)

create data

my_data$obj_1 <- sample(1:500,nrow(my_data),replace=TRUE) my_data$obj_3 <- sample(1000:10000,nrow(my_data),replace=TRUE)

create palette

cols_obj1=colourvalues::colour_values_rgb(my_data$obj_1, palette = "cividis",include_alpha = FALSE) cols_obj2=colourvalues::colour_values_rgb(my_data$obj_3, palette = "cividis",include_alpha = FALSE)

palFunc_objective_1 <- leaflet::colorNumeric(viridis::cividis(nrow(my_data)), my_data$obj_1)

palFunc_objective_2 <- leaflet::colorNumeric(viridis::cividis(nrow(my_data)), my_data$obj_3)

all_groups_leaflet<-c("obj_1","obj_3")

leaflet::leaflet() %>% leaflet::addProviderTiles('Esri.NatGeoWorldMap', group = "Esri.NatGeoWorldMap") %>% leafgl::addGlPolygons(data = my_data, fillOpacity = 0.75, color = cols_obj1, opacity = 1, popup = "obj_1", weight = 0.1, group = "obj_1") %>% leaflet::addLegend(data=my_data, "topright", pal = palFunc_objective_1, values = ~my_data$obj_1 , title = "obj_1", group = "obj_1", opacity = 1)%>%

leafgl::addGlPolygons(data = my_data, fillOpacity = 0.75, color = cols_obj2, opacity = 1, popup = "obj_3", weight = 0.1, group = "obj_3") %>% leaflet::addLegend(data=my_data, "topleft", pal = palFunc_objective_2, values = ~my_data$obj_3 , title = "obj_3", group = "obj_3", opacity = 1)%>% leaflet::addLayersControl(overlayGroups = all_groups_leaflet, options = leaflet::layersControlOptions(collapsed = F)) %>%

leaflet::hideGroup(all_groups_leaflet)%>% leaflet.extras::addFullscreenControl(position = "topleft", pseudoFullscreen = FALSE)