我尝试使用使用任意传单JS插件的例子代码来实现传单并排插件。看起来很简单,到目前为止没有成功。我不知道我做错了什么。非常感谢您的回复。谢谢你,
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);
")
偶然发现了这个问题。
我想你错过了每个贴图图层的addTo()方法。
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);
")
我要赞扬Abuw的回答。如果有人想知道为什么它仍然不起作用,那是因为在onRender JavaScript中有一个不匹配的'{'。
还要确保你的htmlDependency源确实存在。Jsdelivr以application/javascript的格式提供了大多数通过NPM和github可用的js库。使用它而不是您的原始github路径,完整的工作代码应该如下所示:
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);
}")