首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不显示使用rMaps和leaflet routing machine插件的路线

不显示使用rMaps和leaflet routing machine插件的路线
EN

Stack Overflow用户
提问于 2015-02-12 12:38:03
回答 1查看 873关注 0票数 1

我正在遵循这个例子:

https://gist.github.com/ramnathv/9998388

我的目标是画一条路线。使用RStudio时,它工作正常,但当我想使用R控制台时,它只绘制地图,而不绘制路线。有人有解决这个问题的办法吗?我的软件需要使用控制台而不是RStudio来执行。

谢谢!

代码语言:javascript
复制
library(rMaps)
#library(rCharts)
map = Leaflet$new()
map$setView(c(40.74119, -73.9925), 13)
map$tileLayer(provider = 'Stamen.TonerLite')

mywaypoints = list(c(40.74119, -73.9925), c(40.73573, -73.99302))

map$addAssets(
  css = "http://www.liedman.net/leaflet-routing-machine/dist/leaflet-routing-machine.css",
  jshead = "http://www.liedman.net/leaflet-routing-machine/dist/leaflet-routing-machine.min.js"
)

routingTemplate = "
<script>
var mywaypoints = %s
L.Routing.control({
waypoints: [
L.latLng.apply(null, mywaypoints[0]),
L.latLng.apply(null, mywaypoints[1])
]
}).addTo(map);
</script>"

map$setTemplate(
  afterScript = sprintf(routingTemplate, RJSONIO::toJSON(mywaypoints))
)
map$set(width = 800, height = 800)
map

THe闪亮代码如下所示

代码语言:javascript
复制
library(rCharts)
library(shiny) 

runApp(list(
  ui = pageWithSidebar(
    headerPanel("Title"),
    sidebarPanel("MyApp" ),
    mainPanel(
      tabsetPanel(
        tabPanel("Interactive Map", chartOutput("myChart", 'leaflet'))
      )
    )
  ),
  server = function(input, output){
    output$myChart <- renderMap({
      map = Leaflet$new()
      map$setView(c(40.74119, -73.9925), 13)
      map$tileLayer(provider = 'Stamen.TonerLite')

      mywaypoints = list(c(40.74119, -73.9925), c(40.73573, -73.99302))

      map$addAssets(
        css = "http://www.liedman.net/leaflet-routing-machine/dist/leaflet-routing-machine.css",
        jshead = "http://www.liedman.net/leaflet-routing-machine/dist/leaflet-routing-machine.min.js"
      )

      routingTemplate = "
<script>
var mywaypoints = %s
L.Routing.control({
waypoints: [
L.latLng.apply(null, mywaypoints[0]),
L.latLng.apply(null, mywaypoints[1])
]
}).addTo(map);
</script>"

      map$setTemplate(
        afterScript = sprintf(routingTemplate, RJSONIO::toJSON(mywaypoints))
      )
      map$set(width = 800, height = 800)
      map
    })
  }
))
EN

回答 1

Stack Overflow用户

发布于 2015-10-27 08:40:39

为了让这个闪亮的代码正常工作,我做了几个调整(样式有点不对劲...)

代码语言:javascript
复制
library(rCharts)
library(shiny) 

runApp(list(
    ui = pageWithSidebar(
        headerPanel("Title"),
        sidebarPanel("MyApp" ),
        mainPanel(
            tabsetPanel(
                tabPanel("Interactive Map", 
                             chartOutput("baseMap", 'leaflet'),
                             tags$head(tags$style("http://www.liedman.net/leaflet-routing-machine/dist/leaflet-routing-machine.css")),
                             tags$head(tags$script(src="http://www.liedman.net/leaflet-routing-machine/dist/leaflet-routing-machine.min.js")),
                             uiOutput("routeMap"))
            )
        )
    ),

    server = function(input, output){

        output$baseMap <- renderMap({
            baseMap = Leaflet$new()
            baseMap$setView(c(40.74119, -73.9925), 13)
            baseMap$tileLayer(provider = 'Stamen.TonerLite')
            baseMap$set(width = 800, height = 800)
            baseMap
        })

        output$routeMap <- renderUI({

            mywaypoints = list(c(40.74119, -73.9925), c(40.73573, -73.99302))
            tags$body(tags$script(HTML(sprintf(
                "var mywaypoints = %s  
                L.Routing.control({
                waypoints: [
                    L.latLng.apply(null, mywaypoints[0]),
                    L.latLng.apply(null, mywaypoints[1])
                ] }).addTo(map)", RJSONIO::toJSON(mywaypoints)
            ))))
        })
    }
))

这在RStudio和控制台中都有效。不过,我还没有针对.R脚本的解决方案。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28469399

复制
相关文章

相似问题

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