如何防止传单地图在闪亮的应用程序中重置缩放? [英] How to prevent leaflet map from resetting zoom in shiny app?

查看:16
本文介绍了如何防止传单地图在闪亮的应用程序中重置缩放?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我自己和其他人已经构建了一个在线应用程序的原型,以帮助交通规划人员优先考虑新的自行车道资金:

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?

整个应用程序可以在下面的链接中看到,并且应该可以在任何 R 安装上重现,前提是你有正确的包(例如 rgdal、leaflet、ggmap):

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/树/master/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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆