I'm trying to produce a map so that I can choose which groups of data points to display. It does work until I try to 'improve' my legend, ie by including the different types of markers in it.
Right now, my points are displayed correctly, the legend also displays correctly, but when I uncheck one of the groups, nothing happens. Any idea how to fix this? I've spent a couple of hours on it but can't figure it out.
Cheers!
Here is my code:
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
I've found the solution... and it was a pretty silly issue.
Unlike with regular markers, data =
must apparently not be defined in addAwesomeMarkers()
but rather in leaflet()
...
Last bit of working code becomes:
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")
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.