Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust setView() for each group #931

Open
aloboa opened this issue Sep 5, 2024 · 3 comments
Open

Adjust setView() for each group #931

aloboa opened this issue Sep 5, 2024 · 3 comments

Comments

@aloboa
Copy link

aloboa commented Sep 5, 2024

I need to open the map with a relatively wide extent, but then some groups would
need a more zoomed view. Is there a way to define specific setView() for different groups?
For example, in this example:

library(RStoolbox) #for the example satellite image
library(terra)
library(leaflet)
B4  <- project(rast(system.file("external/landsat/LT52240631988227CUB02_B4.TIF", package="RStoolbox")), "EPSG:4326")
ext(B4)
leaflet() %>% 
  setView(lng = -50.5, lat=-3.4 , zoom = 9) %>%
  addTiles() %>% 
  addProviderTiles(providers$Esri.WorldImagery, group = c("Satview", "Landsat")) %>%
  addRasterImage(B4, group = "Landsat") %>% 
  addLayersControl(
    #baseGroups = c("Satview","OSM","r1","a"),
    baseGroups = c("Satview","Landsat"),
    options = layersControlOptions(collapsed = FALSE, autoZIndex = TRUE))

I would need a "zoom to layer" behaviour when selecting the group Landsat.

image

@trafficonese
Copy link

You could just use something like this:

library(leaflet)

df <- st_as_sf(atlStorms2005)
leaflet() %>% 
  addTiles() %>% 
  addCircleMarkers(data = breweries91, group = "breweries91") %>%
  addPolylines(data = df, group="atlStorms2005") %>% 
  addLayersControl(
    baseGroups = c("breweries91", "atlStorms2005"),
    options = layersControlOptions(collapsed = FALSE, autoZIndex = TRUE)
  ) %>%
  htmlwidgets::onRender("
    function(el, x) {
      this.on('baselayerchange', function(e) {
        if (e.name === 'breweries91') {
          this.setView([49, 10], 8);
        }
        if (e.name === 'atlStorms2005') {
          this.setView([40, -50], 4);
        }
      });
    }
  ")

@aloboa
Copy link
Author

aloboa commented Sep 6, 2024

Yes!
The behavior of this code is what I am requesting, but the htmlwidgets code is too involved.
What I am suggesting is a Ruser-friendly function
that would do that behind the scenes.
Thanks

@aloboa
Copy link
Author

aloboa commented Sep 14, 2024

Se also https://stackoverflow.com/questions/78931485/shift-view-to-central-coordinates-in-r-leafletaddrasterimage?noredirect=1#comment139255778_78931485
Anyway, after trying, I am finally not using this tool in my map: Having a "zoom to layer" behavior always active (anytime the layer is selected) is often inconvenient. For example, if we want to display Satview for the zone of interest once we have selected Landsat, the Satview button brings us back to the initial view. We would need a second button next to the one selecting the layer so that the user can "zoom to layer" if needed, but not always

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants