mapboxer icon indicating copy to clipboard operation
mapboxer copied to clipboard

Control over layers/groups

Open Camil88 opened this issue 4 years ago • 4 comments

Hi,

I'm somehow referring to issue #62. I would like to cover 2 issues here:

  • layers control
  • groups

As for the first issue - I would like to hide/show points on the map upon clicking on the button (in short - toggle points on the map). I'm wondering how I could do it using mapboxer since I can't get to layer's properties like visibility. What I can only do, I guess, is setting visibility using set_layout_properties e.g. set_layout_property(ns("map"), layer_id="points", property="visibility", value="visible"). However, I don't know how to first get to the layer's properties (something what's possible in mapbox's function getLayoutProperty). Please take a look at this example from mapbox site: https://docs.mapbox.com/mapbox-gl-js/example/toggle-layers/ where visibility is get/set using getLayoutProperty/setLayoutProperty.

Or maybe there's other way to do it, without getting and setting visibility from a layer. Below is my workable example from #62, where I added some dummy code (in observeEvent) showing how I would 'toggle' points on the map (I have only one button in this example but in my app I have 3 buttons and want to add some logic to observe event, e.g if points 'xyz' are visible one the map then show points 'kkk' etc.).

Another issue is groups. Is it possible to make groups using mapboxer just like it's possible in leaflet? In leaflet I can use function addLayersControl which manage groups quite easily (also enables toggling points on a map). Please take a look at this example where it's shown clearly: https://rstudio.github.io/leaflet/showhide.html

And here is my workable example with commented dummy code:

library(shiny)
library(mapboxer)
library(dplyr)
library(sf)

Sys.setenv(MAPBOX_API_TOKEN =    "pk.eyJ1IjoiaHdsIiwiYSI6ImNramJxY2YxcDV2YXoyeW40YXlvbmUyazQifQ.7HBEvMyrAnVpkKO7MNH7ww")

moduleServer <- function(id, module) {
callModule(module, id)
}

# UI 
mod_btn_UI <- function(id) {

ns <- NS(id)
tagList(
    actionButton(ns("btn"), "Click me!"),
    mapboxerOutput(ns("map"))
    #leafletOutput(ns("map"), width = "100%")
)
}

# Server 
mod_btn_server <- function(id){
moduleServer(id, function(input, output, session) {
    
  ns <- NS(id)
  
    coords <- quakes %>%
        sf::st_as_sf(coords = c("long","lat"), crs = 4326)
         
    output$map <- mapboxer::renderMapboxer({
        mapboxer::mapboxer(
            style = mapboxer::basemaps$Mapbox$light_v10,
            center = c(174.387636,-33.543557),
            logoPosition = "bottom-left",
            pitch = 5,
            padding = 0,
            zoom = 3)
    })
    
    # visibility = mapboxer::get_layout_property(ns(map),layer_id="xyz",'visibility')

    observeEvent(input$btn, {
      # if(visibility == "visible") {
      # mapboxer::set_layout_property(ns("map"),"xyz",'visibility',"none")
      # }
      # else {
        mapboxer::mapboxer_proxy(ns("map")) %>%
            mapboxer::add_circle_layer(
                id = "xyz",
                source = mapboxer::as_mapbox_source(coords),
                circle_color = "#952444",
                circle_opacity = 0.7,
                circle_radius = 6
            ) %>%
            mapboxer::update_mapboxer()
      # }
    })
                   
})
}

# App 

ui <- fluidPage(    
tagList(
    mod_btn_UI("test-btn"))
)

server <- function(input, output, session) {
mod_btn_server("test-btn")    
}

shinyApp(ui = ui, server = server)

Camil88 avatar Mar 10 '21 21:03 Camil88

@Camil88 See shiny-toogle-layer-example where it is done via set_layout_property. You just have to store the current visibility properties of your layers in your shiny app. Furthermore, a layer switcher control is on the way, I already played around with a lot of new controls in the develop and feature/sliders branch. But I have to finish them, so that they can be merged into master. Maybe I can add a addLayerSwitcherControl in the coming days, because I think it is very useful.

crazycapivara avatar Mar 11 '21 06:03 crazycapivara

Thanks for example, I'll look into it. I guess such a switcher would be a nice option either.

Camil88 avatar Mar 11 '21 08:03 Camil88

#49

crazycapivara avatar Mar 12 '21 07:03 crazycapivara

Hi @crazycapivara,

Thanks for this great package. Is there an option to use this without Shiny (or with a mix of R & JS) ?

tvroylandt avatar Feb 15 '23 16:02 tvroylandt