Ad

Change The Default Position Of Zoom Control In Leaflet Map Of Shiny App

- 1 answer

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.

Ad

Answer

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

enter image description here

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)
Ad
source: stackoverflow.com
Ad