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

Add Search by Input Coordinate #154

Closed mhaditama26 closed 3 months ago

mhaditama26 commented 6 years ago

How to put plugin like http://zyzo.github.io/gsoc/fossasia/2015/05/03/gsoc-map-picker-task.html in R leaflet ?

trafficonese commented 3 months ago

There is no need for leaflet.extras for this use-case as far as I understand.

Here is a little working example:

library(shiny)  
library(leaflet)

ui <- fluidPage(
  textInput("lat", "Latitute", ""),
  textInput("lon", "Longitude", ""),
  leafletOutput("map")
)

server <- function(input, output, session) {
  output$map <- renderLeaflet({
    leaflet()  %>% 
      addTiles()
  })

  observe({
    lat <- req(input$lat)
    lat <- tryCatch(as.numeric(lat),
                    warning = function(e) req(FALSE),
                    error = function(e) req(FALSE))
    lon <- req(input$lon)
    lon <- tryCatch(as.numeric(lon), 
                    warning = function(e) req(FALSE),
                    error = function(e) req(FALSE))

    leafletProxy("map") %>%
      setView(lat = lat, lng = lon, zoom = isolate(input$map_zoom)) %>% 
      clearGroup("markers") %>% 
      addMarkers(lat = lat, lng = lon, 
                 group = "markers",
                 labelOptions = labelOptions(permanent = TRUE),
                 label = paste0("Your location: ", lat, ", ", lon)
                 )
  })
}
shinyApp(ui, server)