How to implement input$map_marker_click correctly?

前端 未结 1 1457
没有蜡笔的小新
没有蜡笔的小新 2021-02-10 04:52

Im trying to use shinydashboard with leatlef. Having read this post on SO I have tried several things without success to show corresponding information

1条回答
  •  无人及你
    2021-02-10 05:37

    Your click event is firing as expected, but you need to change how you use it.

    1. The lat / lon values are stored in click$lat and click$lng
    2. You need to use leafletProxy to update the map. You can't just re-use the map object
    3. There is also no function in leaflet called showPopups. You need to use addPopups()

    Your server will look something like

    server = function(input, output, session) {
    
        latitude<-c(35.94077, 35.83770, 35.84545, 35.81584, 35.79387, 36.05600)
        longitude<-c(-78.58010, -78.78084, -78.72444, -78.62568, -78.64262,-78.67600)
        amounts1<-c(27, 44, 34, 46, 25, 15)
        amounts2<-c(34, 52, 35, 78, 14, 24)
        ids<-c("a", "b", "c", "d", "e", "f")
        df<-data.frame(ids,amounts1,amounts2,latitude,longitude)
    
        map = createLeafletMap(session, 'map')
    
        session$onFlushed(once = T, function() {
    
            output$map <- renderLeaflet({
                leaflet(df) %>%
                    addMarkers( ~ longitude,~ latitude)
            })
        })
    
        observe({
            click <- input$map_marker_click
            if (is.null(click))
                return()
    
            print(click)
            text <-
                paste("Lattitude ",
                            click$lat,
                            "Longtitude ",
                            click$lng)
    
            leafletProxy(mapId = "map") %>%
                clearPopups() %>%
                addPopups(dat = click, lat = ~lat, lng = ~lng, popup = text)
    
            # map$clearPopups()
            # map$showPopup(click$latitude, click$longtitude, text)
        })
    }
    
    shinyApp(ui, server)
    

    0 讨论(0)
提交回复
热议问题