2015-02-08 21 views
3

我和其他人建立了一個原型的在線應用,協助交通規劃中優先考慮自行車道的新資金:如何防止傳單映射重置放大閃亮的應用程序?

https://robinlovelace.shinyapps.io/fixMyPath/

我們很高興與結果,並留下了深刻印象閃亮的能力,快速原型概念的而無需編寫單行JavaScript代碼。但是,該應用程序存在一個主要問題,您將通過放大然後調整透明度滑塊來看到:縮放會在每次執行此操作時重置。因此,問題很簡單:如何重寫server.R以使地圖不重置其縮放設置?

整個應用程序可以在下面的鏈接中可以看出,應該是對任何R安裝過程中可重複的,只要你有正確的包裝(如rgdal,傳單,ggmap):

https://github.com/nikolai-b/hackMyRoute/tree/master/R/fixMyPath

更多的上下文請參閱here

回答

5

我有同樣的問題,我想我找到了一些工作:描述here on the Leaflet for R page這裏顯示在SuperZip example

更改您使用LeafletProxy作爲生成地圖的方式。首先,嘗試設置您的renderLeaflet功能是這樣的:

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畫出這樣的線條和圓圈:您需要添加層的ID,以確保

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 

}) 

當您更改參數時,新形狀會替換舊形狀。這樣你就不需要你有的mapOptions(zoomToLimits = "first")

相關問題