【问题标题】:Making Leaflet Map fullscreen in RShiny在 RShiny 中制作传单地图全屏
【发布时间】:2020-08-04 02:21:15
【问题描述】:

我无法在 Shiny 中全屏制作传单地图。虽然我显然可以手动将地图的宽度和高度设置为我的屏幕尺寸,但我希望地图适应它所显示的任何屏幕尺寸。

如何自动调整地图大小?

这是迄今为止我一直在尝试的代码。然而不幸的是,这并不能正确调整高度。

library(shiny)
library(leaflet)

location=c(46.52433,10.12633)

ui <- fluidPage(
  tags$head(tags$style(HTML("#map {height:100%, width:100%;}"))),

  leafletOutput("map")


)

server <- function(input, output, session) {

  output$map = renderLeaflet({
    leaflet() %>% addTiles()  %>% setView(lat = location[1],lng = location[2],zoom = 10) %>% 
      addMarkers(lat = location[1],lng = location[2],popup = "Test") })


}


shinyApp(ui, server)



任何帮助将不胜感激,谢谢!

【问题讨论】:

    标签: css r shiny leaflet


    【解决方案1】:

    尝试将width = "100%", height = "100%" 添加到leafletOutput 并使用fillPage(),如下所示:

    library(shiny)
    library(leaflet)
    
    location=c(46.52433,10.12633)
    
    ui <- fillPage(
      tags$style(type = "text/css", "html, body {width:100%; height:100%}"),
      leafletOutput("map", width = "100%", height = "100%")
    
      )
    
    server <- function(input, output, session) {
    
      output$map = renderLeaflet({
        leaflet() %>% addTiles()  %>% setView(lat = location[1],lng = 
    location[2],zoom = 10) %>% 
          addMarkers(lat = location[1], lng = location[2],popup = "Test") })
    
    
    }
    
    
    shinyApp(ui, server)
    

    【讨论】:

    • 这不起作用——只会出现一个空白页。我不认为实际上将 LeafletOutput() 语句中的高度设置为像素特定高度以外的任何东西都有效,这就是为什么我认为可能需要 CSS 解决方法。
    • type = "text/css", "html, body {width:100%; height:100%}" 内尝试tags$style()
    • 感谢@monarque13 的及时响应。不幸的是,这也不起作用。为了让我们在同一个页面上,这就是我的 tags() 语句:``` tags$head(tags$style(type = "text/css", "html, body {width:100 %; height:100%}")), ``` 没有对 map 元素的引用(尽管我尝试在 body 之后使用 #map,但它也没有)。还有其他想法吗?
    • 很高兴听到。如果您愿意,请考虑支持/接受我的回答。
    • 一直在等着看这两个答案是否有区别,但我现在已将您的答案标记为答案。
    【解决方案2】:

    使用 JavaScript:

    js <- '
    $(document).on("shiny:connected", function(){
      $("#map").css({
        width: window.innerWidth, 
        height: window.innerHeight
      });
      $(window).on("resize", function(e){
        if(e.target instanceof Window){
          $("#map").css({width: window.innerWidth, height: window.innerHeight});
        }
      });
    })
    '
    location=c(46.52433,10.12633)
    
    ui <- fluidPage(
      tags$head(
        tags$style(HTML("html,body {margin: 0; overflow: hidden;}")),
        tags$script(HTML(js))
      ),
      ......
    

    【讨论】:

    • 谢谢,斯蒂芬!所以这和 monarque13 的编辑方法都有效。这两种解决方案有什么不同或优缺点吗?
    猜你喜欢
    • 2014-09-22
    • 2020-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-31
    • 2012-02-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多