using leaflet-side-by-side plugin in R

后端 未结 2 400
再見小時候
再見小時候 2021-01-14 15:41

I tried to implement leaflet-side-by-side plugin using example codes from Using arbitrary Leaflet JS plugins with Leaflet for R. Appears simple, no success so far. I could

相关标签:
2条回答
  • 2021-01-14 16:44

    I would like to commend Abuw for his answer. If anyone is wondering why it still doesn't work, it's because there is an unmatched '{' in the onRender JavaScript.

    Also make sure that your htmlDependency source actually exists. jsdelivr provides most js libraries available through npm and github in application/javascript format. Using that instead of the raw github path you, the full working code should be as follows:

    library(leaflet)
    library(htmltools)
    library(htmlwidgets)
    
    
    LeafletSideBySidePlugin <- htmlDependency("leaflet-side-by-side","2.0.0",
                                              src = c(href="https://cdn.jsdelivr.net/gh/digidem/leaflet-side-by-side@2.0.0/"),
                                              script="leaflet-side-by-side.js")
    
    # A function that takes a plugin htmlDependency object and adds
    # it to the map. This ensures that however or whenever the map
    # gets rendered, the plugin will be loaded into the browser.
    
    registerPlugin <- function(map, plugin) {
      map$dependencies <- c(map$dependencies, list(plugin))
      map
    }
    
    leaflet() %>% addTiles() %>%
      setView(lng = 12, lat = 50, zoom = 4) %>%
      # Register leaflet-side-by-side plugin on this map instance
      registerPlugin(LeafletSideBySidePlugin) %>%
      onRender("
            function(el, x) {
              var mylayer1 = L.tileLayer(
                'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',{
                 maxZoom: 18
                 }).addTo(this);
              var mylayer2 = L.tileLayer(
              '//stamen-tiles-{s}.a.ssl.fastly.net/watercolor/{z}/{x}/{y}.png',{
                 maxZoom: 14
                 }).addTo(this);
            L.control.sideBySide(mylayer1, mylayer2).addTo(this);
            }")
    
    
    0 讨论(0)
  • 2021-01-14 16:46

    just stumble upon this question.

    I think you miss addTo() method for each tile map layer. here is should be.

    leaflet() %>% addTiles() %>%
       setView(lng = 12, lat = 50, zoom = 4) %>%
       # Register leaflet-side-by-side plugin on this map instance
       registerPlugin(LeafletSideBySidePlugin) %>%
       onRender("
            function(el, x) {
              var mylayer1 = L.tileLayer(
                'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',{
                 maxZoom: 18
                 }).addTo(this);
              var mylayer2 = L.tileLayer(
                 '//stamen-tiles-{s}.a.ssl.fastly.net/watercolor/{z}/{x}/{y}.png',{
                 maxZoom: 14
                 }).addTo(this);
            L.control.sideBySide(mylayer1, mylayer2).addTo(this);
            ")
    
    0 讨论(0)
提交回复
热议问题