I am using Shiny app to create leaflet map, but want to change the default position of zoom control from topleft
to topright
.
R leaflet
package sets the default position as topleft
in the source codes.
Following this question: Customize Zoom in/out button in leaflet.js, we can use map.zoomControl.setPosition('topright');
to change position of zoom control.
var map = L.map('map', {
zoomControl: true
});
map.zoomControl.setPosition('topright');
Could I create a R function to set new position of zoomControl
? For example,
zoomControlPosition <- function(map, position = 'topleft') {
# New codes add here
}
I guess it involves some js
, but I have no experience of js
. Thanks for any suggestions.
Try this:
leaflet(options = leafletOptions(zoomControl = FALSE)) %>%
htmlwidgets::onRender("function(el, x) {
L.control.zoom({ position: 'topright' }).addTo(this)
}") %>%
I figure out how to change the position of zoomControl
. You can find this feature from my fork of leaflet package: https://github.com/byzheng/leaflet/commit/fdf9fb159adbc0e36cc2bd7d7b33c72c17c468f6
This is an minimum example to use it:
library(shiny)
library(leaflet)
ui <- fluidPage(
leafletOutput("mymap")
)
server <- function(input, output, session) {
output$mymap <- renderLeaflet({
leaflet() %>%
addTiles() %>%
zoomControlPosition('topright')
})
}
shinyApp(ui, server)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With