popup not working if Polylines are added twice
I want to overlay an already visible Polyline with another one, with a different popup. Interestingly that does not work if the data is part of the already visible layer. I tried the CRAN and current github versions without success. The click event stops here https://github.com/r-spatial/leafgl/blob/51e31b18b5a0cbb06882a019a219caf0d064d1da/inst/htmlwidgets/Leaflet.glify/addGlifyPolylines.js#L20
Interestingly, if the 2nd Polyline uses data = data[11:nrow(data),] instead, the example will work.
Here is an example to illustrate the problem.
library(leaflet)
library(leafgl)
data <- sf::st_as_sf(leaflet::atlStorms2005)
leaflet() %>%
addTiles() %>%
addGlPolylines(
data = data[1:10,],
group = "base",
color = "red",
popup = TRUE) %>%
addGlPolylines(
data = data[1:10,],
# data = data[11:nrow(data),],
color = "blue",
group = "base2",
popup = ~sprintf("I am a special Popup<br><b>Name :</b> %s<br><b>MaxWind :</b> %s",
Name , MaxWind)) %>%
hideGroup("base") %>%
addLayersControl(overlayGroups = "base")
I'll try to have a look soon. My current focus is not on leafgl though, so it may take a while (also regarding your PRs)
It seems that both layers are still active, and glify only "sees" the first one.
Manually setting the first one to active = false with
L.glify.instances.forEach(e => {
if (e.settings.className === "base") {
e.active = false;
}
});
seems to work. I think this might be a Glify issue, by not handling showing/hiding correctly, but I also might be totally wrong.
@trafficonese Were you able to solve the problem ? Does the forEach function goes before the popup block ?
if (addpopup) {
var content = popup === true ? '<pre>'+JSON.stringify(feature.properties,null,' ').replace(/[\{\}"]/g,'')+'</pre>' : popup[idx].toString();
L.popup({ maxWidth: 2000 })
.setLatLng(e.latlng)
.setContent(content)
.openOn(map);
@paragemini I managed to get it to work with the hack mentioned above, but it's not really a fix. I run this JavaScript snippet from within my shinyapp.
Otherwise I think this has to be fixed in Leaflet Glify and cannot be solved here.
@paragemini if that hack does not work for you, you should consider raising this issue at https://github.com/robertleeplummerjr/Leaflet.glify