0

Is there a way to disable scroll wheel zoom events for Leaflet in R, when you do not know what the zoom is in advance?

output$map <- renderLeaflet({
  location <- input$location_id
  lf_bbox <- st_bbox(location) # a bounding box defined by location polygon
  leaflet(options = leafletOptions(cantDoScrollZoom=FALSE) %>%
    fitBounds(lng1 = as.numeric(lf_bbox$xmin), lat1 = as.numeric(lf_bbox$ymin), 
                lng2 =as.numeric(lf_bbox$xmax), lat2 = as.numeric(lf_bbox$ymax) )
})


Things I have tried

leaflet(options = leafletOptions(minZoom = 10, maxZoom = 10))

Setting minZoom and maxZoom could work (see https://stackoverflow.com/questions/58081193/disabling-zooming-when-scrolling-over-leaflet-map-in-r ), but NOT if you don't know the zoom level in advance (if using fitBounds() to set the zoom of the map as above).

leaflet(options = leafletOptions(scrollWheelZoom = FALSE))

Does not work.

leaflet(options = leafletOptions(doubleClickZoom=FALSE))

Leaflet's javascript backend has scrollWheelZoom.disable(), but the current version of R Leaflet does not seem to have a scrollWheelZoom=FALSE option in LeafletOptions()... cf. disabling doubleClickZoom with doubleClickZoom=FALSE does work: Disable double click zoom event in Leaflet for R

leaflet(options = leafletOptions(minZoom = 10, maxZoom = 10)) %>%
  leaflet.extras::suspendScroll()

leaflet.extras has suspendScroll(), which itself does not seem to work in current versions, although this serves a different purpose (i.e. avoiding accidental scrolls), and scroll zoom returns after a while. https://github.com/rstudio/leaflet/issues/464

Any ideas?...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.