Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the default position of zoom control in leaflet map of Shiny app

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.

like image 425
Bangyou Avatar asked Feb 22 '16 00:02

Bangyou


2 Answers

Try this:

leaflet(options = leafletOptions(zoomControl = FALSE)) %>%
    htmlwidgets::onRender("function(el, x) {
        L.control.zoom({ position: 'topright' }).addTo(this)
    }") %>%
like image 131
Polor Beer Avatar answered Sep 22 '22 06:09

Polor Beer


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)
like image 40
Bangyou Avatar answered Sep 21 '22 06:09

Bangyou