首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在R中使用leaflet并排插件

在R中使用leaflet并排插件
EN

Stack Overflow用户
提问于 2016-10-20 03:22:05
回答 2查看 1.3K关注 0票数 4

我尝试使用Using arbitrary Leaflet JS plugins with Leaflet for R中的示例代码来实现leaflet并排插件。看起来很简单,到目前为止还没有成功。我不知道我做错了什么。非常感谢您的回复。谢谢,

代码语言:javascript
复制
library(leaflet)
library(htmltools)
library(htmlwidgets)


LeafletSideBySidePlugin <- htmlDependency("leaflet-side-by-side","2.0.0",
                                          src = c(href="https://github.com/digidem/leaflet-side-by-side"),
                                          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
            })
var mylayer2 = L.tileLayer(
          '//stamen-tiles-{s}.a.ssl.fastly.net/watercolor/{z}/{x}/{y}.png',{
            maxZoom: 14
            })
            L.control.sideBySide(mylayer1, mylayer2).addTo(this);
            ")
EN

回答 2

Stack Overflow用户

发布于 2017-08-22 22:12:04

只是偶然发现了这个问题。

我认为您错过了每个平铺地图层的addTo()方法。这是应该的。

代码语言:javascript
复制
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);
        ")
票数 1
EN

Stack Overflow用户

发布于 2020-04-22 23:23:07

我要赞扬Abuw的回答。如果有人想知道为什么它仍然不能工作,那是因为在onRender JavaScript中有一个不匹配的'{‘。

还要确保您的htmlDependency源代码确实存在。jsdelivr以应用程序/javascript格式通过npm和github提供了大多数可用的js库。使用它而不是原始的github路径,完整的工作代码应该如下所示:

代码语言:javascript
复制
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);
        }")
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40139878

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档