我想在我使用R leaflet package构建的地图上添加一个脉冲标记 这是the plugin我想要使用的。为了做到这一点,我从这个from this github account
编写了以下代码library(leaflet)
library(htmltools)
library(htmlwidgets)
# This tells htmlwidgets about our plugin name, version, and
# where to find the script. (There's also a stylesheet argument
# if the plugin comes with CSS files.)
esriPlugin <- htmlDependency("leaflet-icon-pulse",version = "1.0",
src = c(href = "https://raw.githubusercontent.com/mapshakers/leaflet-icon-pulse/master/src/"),
script = "L.Icon.Pulse.js",stylesheet ="L.Icon.Pulse.css")
# 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() %>% setView(-52.520, 13.185, zoom = 5) %>%
# Register ESRI plugin on this map instance
registerPlugin(esriPlugin) %>%
# Add your custom JS logic here. The `this` keyword
# refers to the Leaflet (JS) map object.
onRender("function(el,x) {
var pulsingIcon = L.icon.pulse({iconSize:[20,20],color:'red'});
var marker = L.marker([52.9167,13.9333],{icon: pulsingIcon}).addTo(this);
}")
然而,它不起作用。我有一个灰色的矩形,而不是一个漂亮的脉冲标记美丽的地图。有人在我的代码中看到了错误吗?
答案 0 :(得分:2)
此代码使用三个备注:
这是代码(调整src参数以匹配您的文件位置):
library(leaflet)
library(htmltools)
library(htmlwidgets)
# This tells htmlwidgets about our plugin name, version, and
# where to find the script. (There's also a stylesheet argument
# if the plugin comes with CSS files.)
esriPlugin <- htmlDependency("leaflet-icon-pulse",version = "1.0",
src = "/home/valter/Desktop/test",
script = "L.Icon.Pulse.js",stylesheet ="L.Icon.Pulse.css")
# 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(-52.520, 13.185, zoom = 5) %>%
# Register ESRI plugin on this map instance
registerPlugin(esriPlugin) %>%
# Add your custom JS logic here. The `this` keyword
# refers to the Leaflet (JS) map object.
onRender("function(el,x) { var pulsingIcon = L.icon.pulse({iconSize:[20,20],color:'red'});
var marker = L.marker([13.185,-52.520],{icon: pulsingIcon}).addTo(this); }")