我尝试使用Using arbitrary Leaflet JS plugins with Leaflet for R中的示例代码来实现leaflet并排插件。看起来很简单,到目前为止还没有成功。我不知道我做错了什么。非常感谢您的回复。谢谢,
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);
")发布于 2017-08-22 22:12:04
只是偶然发现了这个问题。
我认为您错过了每个平铺地图层的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);
")发布于 2020-04-22 23:23:07
我要赞扬Abuw的回答。如果有人想知道为什么它仍然不能工作,那是因为在onRender JavaScript中有一个不匹配的'{‘。
还要确保您的htmlDependency源代码确实存在。jsdelivr以应用程序/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);
}")https://stackoverflow.com/questions/40139878
复制相似问题