And*_*dy 5 javascript r leaflet
是否可以向带有时间轴的地图添加图层控制?(使用 R leaftime 包创建: https: //github.com/timelyportfolio/leaftime)。
在下面的示例中,我想将所有点分组为“食物”,并在单击右上角出现的图层控制选项时使其消失/重新出现。但是当我在 R 中运行代码时,我会在右上角看到一个弹出窗口,我可以单击“食物”作为类别,但单击它并不会导致在我启动计时器时不出现的情况。
我原以为添加 addTimeline( group = 'Food'....) 会让它按照我想要的方式运行,但它似乎没有,我不确定如何通过单击让它出现/消失图层控制按钮。
if(interactive()) {
library(leaflet)
library(leaftime)
library(htmltools)
#Build data.frame with 10 obs + 3 cols
power <- data.frame(
"Latitude" = c(
33.515556, 38.060556, 47.903056, 49.71, 49.041667, 31.934167,
54.140586, 54.140586, 48.494444, 48.494444
),
"Longitude" = c(
129.837222, -77.789444, 7.563056, 8.415278, 9.175, -82.343889,
13.664422, 13.664422, 17.681944, 17.681944
),
"start" = seq.Date(as.Date("2015-01-01"), by = "day", length.out = 10),
"end" = seq.Date(as.Date("2015-01-01"), by = "day", length.out = 10) + 1
)
# use geojsonio to convert our data.frame
# to GeoJSON which timeline expects
power_geo <- geojsonio::geojson_json(power,lat="Latitude",lon="Longitude")
# to style each point differently based on the data
power_styled <- power
# IE does not like alpha so strip colors of alpha hex
power_styled$color <- substr(topo.colors(6)[ceiling(runif(nrow(power),0,6))],1,7)
power_styled$radius <- seq_len(nrow(power_styled)) # ceiling(runif(nrow(power),3,10))
leaflet(geojsonio::geojson_json(power_styled)) %>%
addTiles() %>%
setView(44.0665,23.74667,2) %>%
# addCircleMarkers(
# data = power_styled, lat = ~Latitude, lng = ~Longitude, radius = 11
# ) %>%
addTimeline(
group = 'Food',
timelineOpts = timelineOptions(
styleOptions = NULL, # make sure default style does not override
pointToLayer = htmlwidgets::JS(
"
function(data, latlng) {
return L.circleMarker(
latlng,
{
radius: +data.properties.radius,
color: data.properties.color,
fillColor: data.properties.color,
fillOpacity: 1
}
);
}
"
)
)
) %>%
addLayersControl(overlayGroups = 'Food')
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
305 次 |
| 最近记录: |