如何防止传单地图重置闪亮应用程序中的缩放? [英] How to prevent leaflet map from resetting zoom in shiny app?
问题描述
我自己和其他人已经构建了一个在线应用程序的原型,以帮助运输规划人员确定自行车道的新资金优先次序.
Myself and others have built a prototype of an online application to assist transport planners in prioritising new funding for bicycle paths:
https://robinlovelace.shinyapps.io/fixMyPath/
我们对结果感到满意,并对Shiny无需编写一行JavaScript就能快速为Web部署概念进行原型开发的能力印象深刻.但是,该应用程序存在一个主要问题,您可以通过放大然后调整透明度滑块来查看:每次执行此操作时,缩放都会重置.因此,问题很简单:如何重新编写 server.R
,以便地图不重置其缩放设置?
We are happy with the result and impressed with Shiny's ability to rapidly prototype concepts for web deployment without having to write a single line of JavaScript. However, the app has a major problem that you will see by zooming in and then adjusting the transparency sliders: the zoom resets every time you do this. The question is therefore a simple one: how can a re-write server.R
so that the map does not reset it's zoom settings?
在下面的链接中可以看到整个应用程序,只要您拥有正确的软件包(例如rgdal,leaflet,ggmap),它就可以在任何R安装上都可重现:
The entire app can be seen in the link below and should be reproducible on any R installation, provided you have the right packages (e.g. rgdal, leaflet, ggmap):
https://github.com/nikolai-b/hackMyRoute/树/主/R/fixMyPath
有关更多背景信息,请参见此处.
For more context, please see here.
推荐答案
我有同样的问题,我想我发现了一些可行的方法:
I had the same question and I think I found something that worked:
通过使用LeafletProxy
更改生成地图的方式,如此处的宣传页所述R页,并显示在 SuperZip示例一个>.首先,尝试像这样设置renderLeaflet
函数:
Change the way that you generate the map by using LeafletProxy
as described here on the Leaflet for R page and shown here on the SuperZip example. First, try setting up your renderLeaflet
function like this:
output$map = renderLeaflet(leaflet() %>%
addTiles(urlTemplate = "http://{s}.tile.thunderforest.com/cycle/{z}/{x}/{y}.png") %>%
setView(...) # add the parameters as appropriate set the view or use fitBounds
然后将observe
函数与LeafletProxy
一起使用,以绘制这样的直线和圆:
Then use an observe
function with LeafletProxy
to draw the lines and circles like this:
observe({
leafletProxy("map") %>%
clearShapes() %>%
addPolygons(layerId= "layer1"
, data = leeds
, fillOpacity = 0.4
, opacity = (input$transp_zones)*.4
, fillColor = leeds$color_pcycle
) %>%
addPolyLines(layerId = "layer2"
, data = lfast, color = "red"
, opacity = input$transp_fast
, popup = sprintf("<dl><dt>Distance </dt><dd>%s km</dd><dt>Journeys by bike</dt><dd>%s%%</dd>", round(flows$fastest_distance_in_m / 1000, 1), round(flows$p_cycle * 100, 2))
) %>%
# and so on in a similar fashion for the rest of your shapes
})
您需要添加图层ID,以确保在更改参数时新形状替换旧形状.这样,您就不需要拥有的mapOptions(zoomToLimits = "first")
.
You need to add layer IDs to make sure that the new shapes replace old ones when you change the arguments. This way you should not need the mapOptions(zoomToLimits = "first")
that you had.
这篇关于如何防止传单地图重置闪亮应用程序中的缩放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!