physicalcog
physicalcog

Reputation: 419

Adding padding to fitBounds in leaflet for R

I want to create a pipeline in R that builds a map using leaflet which scales appropriately to the longitude and latitude of any given dataset.

fitBounds() meets my needs if I could add more "padding" around the region.

Looking at the help menu, this could be possible using fitBounds(options = list() but the link provided leads to the leaflet for Javascript help menu.

My question is; how do I set padding in fitbounds?

Here is some example code adapted from the leaflet tutorial;

library(tidyverse)
library(leaflet)

leaflet() %>% 
  addTiles() %>%
  fitBounds(
    lng1=-118.456554, lat1=34.078039,
    lng2=-118.436383, lat2=34.062717
  )

Thanks,

Upvotes: 0

Views: 963

Answers (1)

physicalcog
physicalcog

Reputation: 419

To pass the padding option to fitBounds(), you have to create a list with a vector including your bounds named padding;

i.e. fitbounds(options = list(padding = c(50,50)))

In the example;

library(tidyverse)
library(leaflet)

leaflet() %>% 
  addTiles() %>%
  fitBounds(
    lng1=-118.456554, lat1=34.078039,
    lng2=-118.436383, lat2=34.062717,
    options = list(padding = c(400,400))
  )

Upvotes: 2

Related Questions