leaflet.extras icon indicating copy to clipboard operation
leaflet.extras copied to clipboard

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

Open donvicario opened this issue 5 years ago • 4 comments

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!

donvicario avatar Nov 22 '18 10:11 donvicario

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)

trafficonese avatar Jan 27 '20 11:01 trafficonese

addSearchOSM(options = searchOptions(hideMarkerOnCollapse = TRUE))

chris-holcomb avatar Feb 10 '20 22:02 chris-holcomb

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

Camil88 avatar Sep 24 '21 17:09 Camil88

I've found the solution. Please check #209

Camil88 avatar Sep 30 '21 12:09 Camil88