本文介绍了闪亮:是否只有在点击闪亮的地图后才能启用鼠标滚轮缩放?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只有在第一次点击地图后,才可以启用鼠标滚轮缩放功能.

Is there a way to enable mouse wheel zoom only after first click on map.

我有以下代码,仅在单击地图后才想在其中缩放地图.有办法做到这一点吗?

I have the following code in which I want to zoom the map only after click on the map. Is there a way to do that in shiny?

library(shiny)
library(leaflet)
library(maps)

ui <- fluidPage(
 leafletOutput("CountryMap", width = 1000, height = 500)
)

server <- function(input, output){
   Country = map("world", fill = TRUE, plot = FALSE, regions="USA", exact=TRUE)
   output$CountryMap <- renderLeaflet({
   leaflet(Country) %>% addTiles() %>%
   fitBounds(Country$range[1], Country$range[3], Country$range[2], Country$range[4])%>%
   addPolygons(fillOpacity = 0.6,  smoothFactor = 0.5, stroke = TRUE, weight = 1)
})
}

shinyApp(ui =ui, server = server)

推荐答案

R Leaflet软件包尚未选择禁用zoomControlmouseWheelControl的选项,但根据此 https://github.com/rstudio/leaflet/issues/179 ,但受到链接中Yihui的建议的启发,这是一种动态解决方案根据鼠标单击事件更改maxZoom级别.

R Leaflet package does not have the option to disable zoomControl or mouseWheelControl yet according to this https://github.com/rstudio/leaflet/issues/179, but inspired by Yihui's suggestion from the link, here is a workaround that dynamically changes the maxZoom level depending on mouse click event.

library(shiny)
library(leaflet)
library(maps)

ui <- fluidPage(
    leafletOutput("CountryMap", width = 1000, height = 500)
)

server <- function(input, output){

    Country = map("world", fill = TRUE, plot = FALSE, regions="USA", exact=TRUE)

    # Add a default minZoom and maxZoom of the same value so that the map does not zoom
    output$CountryMap <- renderLeaflet({
        leaflet(Country) %>% addTiles(options=tileOptions(minZoom=4, maxZoom=4)) %>%
            fitBounds(Country$range[1], Country$range[3], Country$range[2], Country$range[4]) %>%
            addPolygons(fillOpacity = 0.6,  smoothFactor = 0.5, stroke = TRUE, weight = 1)
    })

    # Change a reactive value depending on mouse click
    zoom <- reactiveValues(level=4)

    # This records mouse clicks outside polygons
    observeEvent(input$CountryMap_click, {
        zoom$level = 20
    })

    # This records mouse clicks inside polygons
    observeEvent(input$CountryMap_shape_click, {
        zoom$level = 20
    })

    # Change zoom level of the map
    observe({
        if (zoom$level == 20) {
            leafletProxy("CountryMap") %>% clearTiles() %>%
                addTiles(options=tileOptions(minZoom=4, maxZoom=20))
        }
    })

}

shinyApp(ui =ui, server = server)

这篇关于闪亮:是否只有在点击闪亮的地图后才能启用鼠标滚轮缩放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 03:59