5

Is there a way to disable the double click zoom in Leaflet for R?

I've created a Shiny app that changes polygon styles when a country's region is clicked on/off. If you select and immediately deselect the same polygon, the map zooms in. I want to disable this feature so that there's no wonky zooming!

According to leaflet.js documentation, you can disable this feature in JavaScript. Leaflet for R refers to this documentation when I try to search for the solution within R, which is completely unhelpful.

2 Answers 2

7

There is the option doubleClickZoom = FALSE :

leaflet(data,options = leafletOptions(doubleClickZoom= FALSE)) %>% ...

Other options can be found here : Disable leaflet interaction temporary

0
3

I didn't find an option to disable the double click zoom in the R leaflet package. So I tried to disable this option by passing JavaScript code as strings to the shiny web app.

You can try the reproducible example below.

library(leaflet)

#  Make Leaflet map with JavaScript

# // Leaflet providers
leafletProviders <- "var OpenStreetMap_Mapnik = L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
                                                maxZoom: 19
                                                });"
# // Map definition
map <- "var map = L.map('map', {
                  center: [-34.7247, -56.1237],
                  zoom: 8,
                  layers: [OpenStreetMap_Mapnik]
                  });"

# // Basemaps
baseMaps <- "var baseMaps = {'OpenStreetMap': OpenStreetMap_Mapnik};"

# // Add a layer control element to the map
layerControl <- "layerControl = L.control.layers(baseMaps, null, {
                                position: 'topleft'});
                                layerControl.addTo(map);"

ui <- fluidPage(

  leafletOutput("map"),
  HTML(paste("<script>", "\n", leafletProviders, "\n", map, "\n", baseMaps, "\n", layerControl,"\n", "</script>", sep = "")), # Make map JS code 
  HTML("<div id='divHtml' class='shiny-html-output'></div>") # Create empty div with known ID
)

server <- function(input, output, session) {

  output$divHtml <- renderUI({

    interaction <- "map.doubleClickZoom.disable();" # Modify interaction with map
    mapHtml <- HTML(paste("<script>", interaction, "</script>", sep = "")) # Put JS code for interaction with created map in created DIV element

    return(mapHtml)

  })

}

shinyApp(ui, server)

example

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.