无反应的Leaflet的addLayersControl()

时间:2019-07-23 17:17:35

标签: r leaflet

我正在尝试制作地图,以便可以选择要显示的数据点组。它会一直起作用,直到我尝试“改善”我的图例,即在其中包括不同类型的标记。

现在,我的点正确显示,图例也正确显示,但是当我取消选中其中一个组时,什么也没有发生。任何想法如何解决这个问题?我已经花了几个小时,但无法弄清楚。

干杯!

这是我的代码:

library("leaflet")

df <- data.frame(name = c("A", "B", "C"),
                 type = c("Always", "Often", "Never"),
                 city = c("Paris", "Marseille", "Bordeaux"),
                 lat = c(48.9, 43.3, 44.8),
                 long = c(2.3, 5.4, -0.6))

#Create list of icons
IconSet <- awesomeIconList(
  "Always"   = makeAwesomeIcon(icon = "map-marker", markerColor = "green", library = "glyphicon"),
  "Often"   = makeAwesomeIcon(icon = "map-marker", markerColor = "blue", library = "glyphicon"),
  "Never" = makeAwesomeIcon(icon = "map-marker", markerColor = "red", library = "glyphicon"))

#Create groups
groups <- c("Always" <- "<div style='position: relative; display: inline-block' class='awesome-marker-icon-green awesome-marker'><i class='glyphicon glyphicon-map-marker icon-white '></i></div>Always displayed",
            "Often" <- "<div style='position: relative; display: inline-block' class='awesome-marker-icon-blue awesome-marker'><i class='glyphicon glyphicon-map-marker icon-white '></i></div>Often displayed",
            "Never" <- "<div style='position: relative; display: inline-block' class='awesome-marker-icon-red awesome-marker'><i class='glyphicon glyphicon-map-marker icon-white '></i></div>Never displayed")

#Create map
m <- leaflet() %>%
  addTiles(options = providerTileOptions(minZoom = 5, maxZoom = 9)) %>% 
  setView(lng = 2.4, lat = 46.5, zoom = 5) %>%
  addAwesomeMarkers(icon = ~IconSet[type],
                    clusterOptions = markerClusterOptions(freezeAtZoom = 9),
                    data = df, lng = ~long, lat = ~lat,
                    group = ~groups[type]) %>% 
  addLayersControl(overlayGroups = groups, options = layersControlOptions(collapsed = FALSE), position = "topleft")
m

1 个答案:

答案 0 :(得分:0)

我找到了解决方案……这是一个非常愚蠢的问题。

与常规标记不同,data =显然不必在addAwesomeMarkers()中定义,而应在leaflet() ...

中定义

工作代码的最后一部分变为:

m <- leaflet(data = df) %>% 
addTiles(options = providerTileOptions(minZoom = 5, maxZoom = 9)) %>% 
setView(lng = 2.4, lat = 46.5, zoom = 5) %>% 
addAwesomeMarkers(icon = ~IconSet[type], group = ~groups[type]) %>% 
addLayersControl(overlayGroups = groups, options = layersControlOptions(collapsed = FALSE), position = "topleft")