问题描述
在设置地图时,传单提供了一个选项,用于隐藏缩放控件
Leaflet provides an option, when setting up your map, to hide the zoom controls
leaflet(options = leafletOptions(zoomControl = FALSE)
但是,我想在创建地图后调用此选项,以便用户可以在没有缩放控件的情况下下载地图,而无需从头开始重新创建其他版本的地图.
However, I would like to call this option after having already created a map so that a user can download the map without the zoom controls and without me having to re-create a different version of the map from scratch.
这是我的应用程序的一个简单版本:
Here's a simple version of my app at the moment:
library(shiny)
library(tidyverse)
library(leaflet)
library(mapview)
ui <- fluidPage(
fluidPage(
leafletOutput(outputId = "map"),
downloadButton(outputId = "save")
)
)
server <- function(input, output, session) {
map <- reactive({
leaflet() %>%
addTiles()
})
output$map <- renderLeaflet({
map()
})
output$save <- downloadHandler(
filename = "map.jpeg",
content = function(file){
latRng <- range(input$map_bounds$north,
input$map_bounds$south)
lngRng <- range(input$map_bounds$east,
input$map_bounds$west)
map() %>%
setView(lng = (lngRng[1] + lngRng[2])/2,
lat = (latRng[1] + latRng[1])/2,
zoom = input$map_zoom) %>%
### HERE ###
mapshot(file = file)
}
)
}
shinyApp(ui, server)
我希望能够在注释### HERE ###
的位置添加一行代码,以关闭缩放控件.在我的实际代码中,显示的地图确实非常复杂,具有很多选项,并且我不想为了两次删除所有代码而仅仅为了在初次调用leaflet()
时删除缩放控件.
I'd like to be able to add a line of code where I've commented ### HERE ###
that would turn off zoom controls. In my actual code the displayed map is really complex with lots of options and I wouldn't want to have all that code twice just for the sake of removing zoom controls in the initial call to leaflet()
.
谢谢
推荐答案
您可以这样做:
library(shiny)
library(tidyverse)
library(leaflet)
library(mapview)
ui <- fluidPage(
fluidPage(
leafletOutput(outputId = "map"),
downloadButton(outputId = "save")
)
)
server <- function(input, output, session) {
map <- reactive({
leaflet() %>%
addTiles()
})
output$map <- renderLeaflet({
map()
})
output$save <- downloadHandler(
filename = "map.jpeg",
content = function(file){
latRng <- range(input$map_bounds$north,
input$map_bounds$south)
lngRng <- range(input$map_bounds$east,
input$map_bounds$west)
m = map() %>%
setView(lng = (lngRng[1] + lngRng[2])/2,
lat = (latRng[1] + latRng[1])/2,
zoom = input$map_zoom)
m$x$options = append(m$x$options, list("zoomControl" = FALSE))
mapshot(m, file = file)
}
)
}
shinyApp(ui, server)
,它将在创建地图后更新传单选项.我会将其合并到mapshot
函数中,以有选择地删除zoomControl.
which is updating the leaflet options after map creation. I will incorporate this in the mapshot
function to optionally remove the zoomControl.
这篇关于从Shiny中渲染的传单地图中删除缩放控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!