如何修复(锁定)传单地图视图缩放和居中?

问题描述 投票:0回答:1

我正在构建一个与此one类似的应用程序。在地图上,如果您放大然后更改滑块/输入,缩放级别会自动重置为默认值。我想在不更改缩放级别的情况下渲染地图的新实例,直到用户将其更改回来。理想情况下,我会添加一个按钮将缩放重置为原始设置。

我查看了这些帖子:123

第三个链接中的代码对我来说有意义,但仍然不起作用。根据评论,Thier code 应该解决了缩放问题,但没有解决居中问题 - 两者都不适合我。下面,我修改了original应用程序以尽可能接近我的应用程序。我还实现了两项更改,试图实现所需的地图视图行为 - 我添加了两个反应功能:缩放和居中。这是修改后的代表:

library(shiny)
library(ggplot2)
library(plotly)
library(leaflet)

qDat <- quakes

ui <- fluidPage(
  titlePanel("pyData Shiny Demo"),
  sidebarLayout(
    sidebarPanel(
      h3("Fiji Earthquake Data"),
      selectInput("select01", "Select earthquakes based on:",
                  choices=c("Magnitude"="mag",
                            "Depth"="depth"),
                  selected="mag"),
      conditionalPanel(condition="input.select01=='mag'",
                       sliderInput("sld01_mag",
                                   label="Show earthquakes of magnitude:", 
                                   min=min(qDat$mag), max=max(qDat$mag),
                                   value=c(min(qDat$mag),max(qDat$mag)), step=0.1)
      ),
      conditionalPanel(condition="input.select01=='depth'",
                       sliderInput("sld02_depth",
                                   label="Show earthquakes of depth:", 
                                   min=min(qDat$depth), max=max(qDat$depth),
                                   value=c(min(qDat$depth),max(qDat$depth)), step=5)
      ),
      plotlyOutput("hist01")
      
    ),
    mainPanel(
      leafletOutput("map01"),
      dataTableOutput("table01")
    )
  )
)

server <- shinyServer(function(input, output) {
  
  qSub <- reactive({
    if (input$select01=="mag"){
      subset <- qDat[qDat$mag>=input$sld01_mag[1] & qDat$mag<=input$sld01_mag[2],]
    }else{
      subset <- qDat[qDat$depth>=input$sld02_depth[1] & qDat$depth<=input$sld02_depth[2],]
    }
    subset
  })
  
  output$hist01 <- renderPlotly({
    ggplot(data=qSub(), aes(x=stations))+
      geom_histogram(binwidth=5)+
      xlab("Number of Reporting Stations")+ 
      xlim(min(qDat$stations), max(qDat$stations))+
      ylab("Count")+
      ggtitle("Earthquakes near Fiji")
  })
  
  output$table01 <- renderDataTable({
    qSub()
  })

  zoom <- reactive({
    ifelse(is.null(input$map01_zoom),3,input$map01_zoom)
  })

  center <- reactive({
    ifelse(is.null(input$map01_bounds),
           c(179.462, -20.64275),
           c((input$map01_bounds$bounds$north + input$map01_bounds$bounds$south)/2.0, 
             (input$map01_bounds$bounds$east + input$map01_bounds$bounds$west)/2.0))
  })
  
  
  pal <- colorNumeric("YlOrRd", domain=c(min(quakes$mag), max(quakes$mag)))
  
  output$map01 <- renderLeaflet({
  leaflet(data=qSub()) %>% 
      addTiles() %>%
      addLegend("bottomright", pal = pal, values = ~mag,
                title = "Earthquake Magnitude",
                opacity = 1)
  })
  
  observe({
    
    leafletProxy("map01") %>%
      clearShapes() %>%
      #setView(lng = 179.462, lat =  -20.64275, zoom = 3) %>%
      setView(lng = center()[1],
              lat = center()[2],
              zoom = zoom()) %>%
      addCircleMarkers(
        data=qSub(),
        radius = 2,
        color = ~pal(mag),
        stroke = FALSE, fillOpacity = 1, popup=~as.character(mag))
  })
  
})

shinyApp(ui = ui, server = server)

您对如何实现这一目标有什么建议吗?

r shiny r-leaflet
1个回答
4
投票

你就快到了。你的应用程序中只有一个错误:

你需要改变

center <- reactive({
    ifelse(is.null(input$map01_bounds),
           c(179.462, -20.64275),
           c((input$map01_bounds$bounds$north + input$map01_bounds$bounds$south)/2.0, 
             (input$map01_bounds$bounds$east + input$map01_bounds$bounds$west)/2.0))
  })

      center <- reactive({
        
        if(is.null(input$map01_center)){
          return(c(179.462, -20.64275))
          }else{
            return(input$map01_center)
        }

  })

第一个原因是当向量长度超过 1 时

ifelse
不起作用,第二个原因是
input$map01_center
为您提供中心。

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.