带有R小册子包的脉冲标记插件

时间:2016-09-18 07:35:08

标签: r plugins leaflet

我想在我使用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);
           }")

然而,它不起作用。我有一个灰色的矩形,而不是一个漂亮的脉冲标记美丽的地图。有人在我的代码中看到了错误吗?

1 个答案:

答案 0 :(得分:2)

此代码使用三个备注:

  • js和css文件存储在本地
  • 图标在RStudio查看器中正确显示但不会产生脉动
  • 使用Viewer中的“在新窗口中显示”选项一切正常(在Firefox 48.0和Chrome 53.0.2785.116(64位)中测试)

这是代码(调整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); }")