首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在R leaflet中实现Leaflet插件"Leaflet-Pegman“

如何在R leaflet中实现Leaflet插件"Leaflet-Pegman“
EN

Stack Overflow用户
提问于 2020-09-02 16:34:11
回答 1查看 186关注 0票数 1

我想在我的应用程序中使用谷歌街景。我正在用传单画我的地图。我发现了一个很棒的传单插件"Leaflet Pegman“。

我如何将这个插件实现到一个闪亮的应用程序中?

我试着使用这个explanation。我还找到了另一个R-package (googleway),但在我的例子中,我想使用leaflet。

谁能为我提供一个有效的例子。这是我现在的代码:

代码语言:javascript
复制
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)

非常非常感谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-09-02 17:51:44

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

这段代码现在适用于我了:

代码语言:javascript
复制
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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63701699

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档