0

Here's what I tried

server <- function(input, output) {
  observe({
    output$mymap <- renderLeaflet({
      leaflet() %>%
        addTiles() %>%
        fitBounds(-124.7666, 49.4000, -67.0583, 25.0666)%>%
        setView(-95.91245, 37.2333, zoom = 3)
    })
    
    click = input$mymap_click
    if(is.null(click))
      return()
    
    leafletProxy('mymap')%>%addMarkers(lng = click$lng,
                                       lat = click$lat)%>%
      setView(lng = click$lng,
              lat = click$lat, zoom =7)
    
    output$text <- renderText(paste(click$lng,click$lat))
  })
}

ui <- fluidPage(textOutput("text"),
                leafletOutput("mymap"))

shinyApp(ui = ui, server = server)

But instead of a reactive output text, I want something which is dynamic i.e., map should change with change in lat, lon value and vice versa

Here's a sample representation from https://psl.noaa.gov/eddi/
Here's a sample representation

1 Answer 1

0

If what you want is for the map to center on the clicked marker, move the lines from click = ... to setView in its own observeEvent(). Also, you do not need to wrap everything in an observe().

server <- function {
  output$mapmap <- renderLeaflet(...)

  observeEvent(input$mymap_click) {
    click = ...
    ...
    leafletProxy(...) %>%
      setView(...)

  }

  output$text <- renderText(...)

}
1
  • Maybe I got my wording wrong. I am precisely looking for both map and lat lon interface to be reactive to each other, i.e. 1. If we input lat and long value (as an input interface) it should add as a marker on the map Also, 2. If we click on the map, it should show the lat and long value (in the input interface itself) of the marker on the map. Commented Jun 24, 2021 at 18:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.