joyBeatric
问题:我想在shiny中利用leaflet(或者其他地图包)动态的展示一系列连续点的轨迹(动画的形式,不是不断的重新画图再显示出来)?
(1)我在stack overflow上找到了如下的代码可以大致实现一部分功能:
library(htmlwidgets)
library(htmltools)
library(leaflet)
library(geojsonio)
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" =
as.Date(c("1971-09-15", "1971-12-01", "1972-02-01", "1972-02-01", "1972-02-01", "1972-02-01", "1972-04-01", "1972-04-01", "1972-04-24", "1972-04-24"))
)
# set start same as end
# adjust however you would like
power$end <- power$start
# use geojsonio to convert our data.frame
# to GeoJSON which timeline expects
power_geo <- geojson_json(power,lat="Latitude",lon="Longitude")
# create a leaflet map on which we will build
leaf <- leaflet() %>%
addTiles()
# add leaflet-timeline as a dependency
# to get the js and css
leaf$dependencies[[length(leaf$dependencies)+1]] <- htmlDependency(
name = "leaflet-timeline",
version = "1.0.0",
src = c("href" = "http://skeate.github.io/Leaflet.timeline/"),
script = "javascripts/leaflet.timeline.js",
stylesheet = "stylesheets/leaflet.timeline.css"
)
# use the new onRender in htmlwidgets to run
# this code once our leaflet map is rendered
# I did not spend time perfecting the leaflet-timeline
# options
leaf %>%
setView(44.0665,23.74667,2) %>%
onRender(sprintf(
'function(el,x){
var power_data = %s;
var timeline = L.timeline(power_data, {
pointToLayer: function(data, latlng){
var hue_min = 120;
var hue_max = 0;
var hue = hue_min;
return L.circleMarker(latlng, {
radius: 12,
color: "red",
fillColor: "red"
});
},
steps: 1000,
duration: 10000,
showTicks: true
});
timeline.addTo(HTMLWidgets.find(".leaflet"));
}
',
power_geo
))
但是在嵌入到shiny中以后无法显示地图,不知道如何解决。
(2)此外,不知道是否有别的方法可以解决在利用shiny在地图中绘制动态轨迹的方法?