trafficonese / leaflet.extras

Extra functionality for leaflet R package.
https://trafficonese.github.io/leaflet.extras/
GNU General Public License v3.0
213 stars 74 forks source link

How do I make the search markers from addSearchOSM() disappear again? #158

Closed donvicario closed 5 months ago

donvicario commented 5 years ago

Hi Bhaskar,

I tried to use the addSearchOSM() feature in my leaflet map (html, not shiny) and it does what it's supposed to do. But after I have searched for a place, the search marker remains visible and cannot be removed. The same is with addReverseSearchOSM().

I have noticed the function removeSearchOSM(), but I do not know how to use it. Can you help me?

Thank you!

trafficonese commented 4 years ago

Indeed this doesnt seem to work and the browser console throws this error:

this.searchControlOSM.removeFrom is not a function

I made a little shinyapp and tried to remove the elements with shinyjs. That makes it possible to remove the addSearchOSM control button, but the marker remains on the map.

The addReverseSearchOSM control can also be removed with simple JavaScript but the Event handler remains active.

library(shiny)
library(shinyjs)
library(leaflet)
library(leaflet.extras)

ui <- fluidPage(
  useShinyjs(),
  leafletOutput("map")
  , actionButton("clearOSM", "clearOSM")
  , actionButton("removeSearchOSM", "removeSearchOSM - Not working")
  , actionButton("clearReverseSearchOSM", "clearReverseSearchOSM")
  , actionButton("removeReverseSearchOSM", "removeReverseSearchOSM")
)

server <- function(input, output, session) {
  output$map <- renderLeaflet({
    leaflet() %>% 
      addTiles() %>% 
      addSearchOSM() %>% 
      addReverseSearchOSM(group = "revosm")
  })
  observeEvent(input$clearOSM, {
    runjs('$(".leaflet-control-search").remove()')
  })
  observeEvent(input$removeSearchOSM, {
    leafletProxy("map") %>% 
      removeSearchOSM()   ## NOT WORKING
  })
  observeEvent(input$clearReverseSearchOSM, {
    leafletProxy("map") %>% 
      clearGroup("revosm")
  })
  observeEvent(input$removeReverseSearchOSM, {
    ## Event Handler is not removed!!!
    runjs('$("#reverseSearchOSM").remove()')
    leafletProxy("map") %>% 
      clearGroup("revosm")
  })
}

shinyApp(ui, server)
chris-holcomb commented 4 years ago

addSearchOSM(options = searchOptions(hideMarkerOnCollapse = TRUE))

Camil88 commented 2 years ago

@ideamotor it doesn't work. I tried everything but in vain

Camil88 commented 2 years ago

I've found the solution. Please check #209