Использование плагина SelectAreaFeature с Leaflet для R - PullRequest
3 голосов
/ 10 января 2020

Я пытаюсь использовать плагин SelectAreaFeature Leaflet JS с Leaflet для R.

Я успешно использовал другие плагины Leaflet JS, но не плагин SelectAreaFeature. Вот MRE (требуется SelectAreaFeature js file ):

library(leaflet)
library(htmltools)
library(shiny)

selectarea <- htmlDependency("SelectAreaFeature", "1",
                             src = c(file = paste0(getwd(),"/src")), # update path
                             script = "Leaflet.SelectAreaFeature.js")

registerPlugin <- function(map, plugin) {
  map$dependencies <- c(map$dependencies, list(plugin))
  map
}

server = function(input, output, session) {
  output$map <- renderLeaflet({
    leaflet() %>%
      addProviderTiles(providers$CartoDB.Positron) %>%
      setView(lng=9.4, lat=56.3, zoom = 8) %>%
      registerPlugin(selectarea)
  })
}

ui <- fluidPage(
  tags$script(HTML(
    '$("#enable-button").click(function(){
        selectfeature = map.selectAreaFeature.enable();

        selectfeature.options.color = "#663399" ;
        selectfeature.options.weight = 3 ;
      });'
  )),
  leafletOutput("map",height=700),
  p(),
  actionButton(inputId = 'enable-button',icon = icon("draw-polygon"),
                label = 'Select area')
)

shinyApp(ui, server)

Использование Chrome DevTools показывает, что плагин не является загруженным источником. Я подозреваю, что проблема может быть в том, как написан этот плагин. SelectAreaFeature. js имеет эту ключевую строку:

L.Map.addInitHook('addHandler', 'selectAreaFeature', L.SelectAreaFeature);

Другой плагин Leaflet JS, zoomdisplay , который я успешно использовал в Leaflet для R, имеет структуру, отличную от его следующая строка addInitHook:

L.Map.addInitHook(function () {
    if (this.options.zoomDisplayControl) {
        this.zoomDisplayControl = new L.Control.ZoomDisplay();
        this.addControl(this.zoomDisplayControl);
    }
});
...