leaflet easy-button detect

Hi all,

I want to find the position of my user using leaflet in a shiny app. Using the example from the doc here and the here I can access the position using input$MAPID_center.

However I would like to detect the click on the geolocate button (to use an observeEvent & update the value only when the user uses the button). Unfortunately I can't figure out how doing that...

Sorry if it's pretty straightforward and thank you for your help,
Cheers

Not straight forward, but possible.

Using this doc, Shiny - How to send messages from the browser to the server and back using Shiny, and the onClick param for the easy button, you can set a shiny input value from within the on-click method. In the example below, I set my_easy_button to either "frozen" or "free".

Let me know how this works out for you!

library(shiny)
library(leaflet)

r_colors <- rgb(t(col2rgb(colors()) / 255))
names(r_colors) <- colors()

ui <- fluidPage(
  leafletOutput("mymap")
)

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

  observeEvent(input$my_easy_button, {
    str(input$my_easy_button)
  })

  output$mymap <- renderLeaflet({
    leaflet() %>% 
      addTiles() %>%
      addMarkers(data=quakes,
        clusterOptions = markerClusterOptions(),
        clusterId = "quakesCluster") %>%
      addEasyButton(easyButton(
        states = list(
          easyButtonState(
            stateName="unfrozen-markers",
            icon="ion-toggle",
            title="Freeze Clusters",
            onClick = JS("
              function(btn, map) {
                var clusterManager =
                  map.layerManager.getLayer('cluster', 'quakesCluster');
                clusterManager.freezeAtZoom();
                btn.state('frozen-markers');
                Shiny.onInputChange('my_easy_button', 'frozen');
              }")
          ),
          easyButtonState(
            stateName="frozen-markers",
            icon="ion-toggle-filled",
            title="UnFreeze Clusters",
            onClick = JS("
              function(btn, map) {
                var clusterManager =
                  map.layerManager.getLayer('cluster', 'quakesCluster');
                clusterManager.unfreeze();
                btn.state('unfrozen-markers');
                Shiny.onInputChange('my_easy_button', 'free');
              }")
          )
        )
      ))
  })
}

shinyApp(ui, server)

33%20AM

2 Likes

Woow that's awesome! Thank you very much!!

Since I just need to know if it's clicked it's even easier than your example! Thank you for the reference as well, I've seen this article but it seems I should read it carefully to understand & probably learn some JS...

Thanks again! (& happy new year)
Cheers

2 Likes

Correct. You could probably just use the code below within your app.

onClick = JS("
  function(btn, map) {
    Shiny.onInputChange('my_easy_button', 'clicked');
  }"
)

Happy New Year!

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.