如何在R Leaflet中实现传单插件“ Leaflet-Pegman”

问题描述

我想在我的闪亮应用程序中使用Google Streetview。我正在使用传单来绘制地图。我找到了这个很棒的传单插件Leaflet Pegman”。

如何将该插件实现到r闪亮的应用程序中?

我尝试使用此explanation。 我还找到了另一个R包(googleway),但就我而言,我想改用传单。

有人可以给我提供一个可行的例子吗?现在这是我的代码

library(shiny)
library(leaflet)
library(htmltools)
library(htmlwidgets)

Googlekey <- "api_key_here"

PluginPegman <- htmlDependency(name = "Pegman",version = "0.1.4",src = c(href = "https://unpkg.com/leaflet-pegman/0.1.4/"),script = "leaflet-pegman.js",stylesheet = "leaflet-pegman.css")

registerPlugin <- function(map,plugin) {
  map$dependencies <- c(map$dependencies,list(plugin))
  map
}

ui <- bootstrapPage(
  tags$style(type = "text/css","html,body {width:100%;height:100%}"),leafletoutput("map",width = "100%",height = "100%")
)

server <- function(input,output) {
  
  output$map <- renderLeaflet({
    leaflet() %>%
      # Set view to the Netherlands
      setView(5.41077,52.13012,zoom = 8) %>%
      addProviderTiles(providers$OpenStreetMap,group = "OSM") %>%
      
      registerPlugin(PluginPegman)  %>%
      onRender("function() {
      var pegmanControl = new L.Control.Pegman({
      position: 'bottomright',// position of control inside the map
      theme: 'leaflet-pegman-v3-default',// or 'leaflet-pegman-v3-small'});
      pegmanControl.addTo(map);
               }")
  })
}

shinyApp(ui = ui,server = server)

非常感谢您。

解决方法

我必须在UI的head中包括leaflet-pegman js文件,它才能正常工作。我还编辑了htmlDependency中的链接,因为它没有引用正确的链接。

此代码现在对我有效:

library(shiny)
library(leaflet)
library(htmltools)
library(htmlwidgets)

Googlekey <- "api_key_here"

PluginPegman <- htmlDependency(name = "Pegman",version = '0.1.5',src = c(href = "https://unpkg.com/leaflet-pegman"),script = "leaflet-pegman.js",stylesheet = "leaflet-pegman.css")

registerPlugin <- function(map,plugin) {
    map$dependencies <- c(map$dependencies,list(plugin))
    map
}

ui <- bootstrapPage(
    tags$head(
        tags$script(src='https://unpkg.com/leaflet-pegman@0.1.5/leaflet-pegman.js'),tags$style(type = "text/css","html,body {width:100%;height:100%}")
    ),leafletOutput("map",width = "100%",height = "100%")
)

server <- function(input,output) {
    
    output$map <- renderLeaflet({
        leaflet() %>%
            # Set view to the Netherlands
            setView(5.41077,52.13012,zoom = 8) %>%
            addProviderTiles(providers$OpenStreetMap,group = "OSM") %>%
            registerPlugin(PluginPegman)  %>%
            onRender("function(el,x) {
      var pegmanControl = new L.Control.Pegman({
      position: 'bottomright',theme: 'leaflet-pegman-v3-default',apiKey: YOUR API KEY});
      pegmanControl.addTo(this);}")
    })
}

shinyApp(ui = ui,server = server)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...