【问题标题】:Plotting waypoint routes with googleway in Shiny App在 Shiny App 中使用 googleway 绘制航点路线
【发布时间】:2018-09-03 12:19:47
【问题描述】:

我正在一个 Shiny 应用程序中使用 google-way。

我从这个示例开始:Drawing journey path using leaflet in R 并对其进行了一些修改,以在 Destination 和 Origin 之间添加 2 个额外的航点,并让 google_directions 呈现它们之间的最佳路线。

绘制的路线确实是最佳的,我可以将其复制到 5-6-7 或 10 个航路点。但是,在每种情况下,要绘制路线,我都需要为所有航路点输入地址。如果某些航点留空,应用程序会返回错误,我不知道如何解决这个问题。我想应该使用某种“if”运算符,但不确定如何以及在哪里。

这是我的代码:

library(shiny)
library(googleway)

ui <- navbarPage("APP NAME", position = c("static-top"),
tabPanel("MAP",
       google_mapOutput(outputId = "mapWarsaw"),
       textInput(inputId = "origin", label = "Departure point"
       ),
       textInput(inputId = "waypoint", label = "Waypoint 1"
       ),
       textInput(inputId = "waypoint2", label = "Waypoint 2",
       ),
       textInput(inputId = "destination", label = "Destination point"
       ),
       actionButton(inputId = "getRoute", label = "Get Route")
       )
)

server <- function(input, output, session) {


map_key <- '<APIKEY>'
api_key <- '<APIKEY>'

df_route <- eventReactive(input$getRoute,{

print("getting route")

o <- input$origin
w <- input$waypoint
q <- input$waypoint2
d <- input$destination

return(data.frame(origin = o, waypoint = w, waypoint2 = q, destination = d, stringsAsFactors = F))

})


output$mapWarsaw <- renderGoogle_map({
df <- df_route()
print(df)
if(df$origin == "" | df$waypoint == ""| df$waypoint2 == "" | df$destination == "")
  return()

res <- google_directions(key = api_key,
                         origin = df$origin,
                         waypoints = list(stop = df$waypoint,
                                          stop = df$waypoint2),
                         destination = df$destination,
                         optimise_waypoints = TRUE,
                         mode = "driving")

df_route <- data.frame(route = res$routes$overview_polyline$points)

google_map(key = map_key, search_box = TRUE, scale_control = TRUE, height = 1000) %>%
  add_traffic()%>%
  add_polylines(data = df_route,
                polyline = "route",
                stroke_colour = "#FF33D6",
                stroke_weight = 7,
                stroke_opacity = 0.7,
                info_window = "New route",
                load_interval = 100)
})
}


shinyApp(ui, server)

数据文件:

popupcontent;label;lat;lng          
Location 1;Location Label 1;52.2572126;20.9845778   
Location 2;Location Label 2;52.2423875;21.0244197   
Location 3;Location Label 3;52.238209;21.0202826    
Location 4;Location Label 4;52.23048060000001;21.0108891
Location 5;Location Label 5;52.231755;21.006482 
Location 6;Location Label 6;52.2376719;21.0158829A7 
Location 7;Location Label 7;52.2540379;21.0347079   
Location 8;Location Label 8;52.2550605;21.052299    
Location 9;Location Label 9;52.247861;21.049922 
Location 10;Location Label 10;52.2720819;21.0151038
Location 11;Location Label 11;52.2582368;21.0396915
Location 12;Location Label 12;52.2569792;21.0294134
Location 13;Location Label 13;52.2549106;21.0475752
Location 14;Location Label 14;52.206521;20.999852   
Location 15;Location Label 15;52.2937449;21.0333861
Location 16;Location Label 16;52.2878479;21.0412171
Location 17;Location Label 17;52.305039;21.0584014  

【问题讨论】:

  • 是的,我修改了您的示例以包含航路点。这是我的代码:dropbox.com/s/pe7be91k7i7a86w/test_code.r?dl=0 我浏览了您的文档和插图,我对标记非常熟悉。我从带有 lat/lon 的 .csv 文件中添加了一些到地图中。不过,我正在努力解决两个问题: 1. 如何可视化由 google_directions 优化的航点的顺序?例如通过数字标记。 2.如果没有选择一个或多个航点,如何使应用程序工作?如果未选择两个航点,则会出现错误。我想应该使用一些'if'运算符......
  • 非常感谢@SymbolixAU!这真的很有帮助。我修改了问题 - 希望现在可以了。
  • 谢谢。当然,请稍等一下数据。
  • 无法决定重新投票,因为没有数据。
  • 然后将其最小化 - 数据和与之相关的代码已被删除。

标签: r google-maps shiny googleway


【解决方案1】:

我所做的一些 cmets 和更改:

  • 使用单个observeEvent() 观察按钮单击并更新地图
  • 添加了 update_google_map() 以在每次搜索路线时清除和更新地图'
  • Google API 可以处理空航点,因此不需要额外的错误处理

library(shiny)
library(googleway)

ui <- navbarPage("APP NAME", position = c("static-top"),tabPanel("MAP",
               google_mapOutput(outputId = "mapWarsaw"),
               textInput(inputId = "origin", label = "Departure point"),
               textInput(inputId = "waypoint", label = "Waypoint 1"),
               textInput(inputId = "waypoint2", label = "Waypoint 2"),
               textInput(inputId = "destination", label = "Destination point"),
               actionButton(inputId = "getRoute", label = "Get Route")
)
)

server <- function(input, output, session) {

  map_key <- "MAP_KEY"
  api_key <- "API_KEY"

  output$mapWarsaw <- renderGoogle_map({
    google_map(key = map_key, 
               search_box = TRUE, 
               scale_control = TRUE, 
               height = 1000) %>%
      add_traffic()
  })

  observeEvent(input$getRoute,{

    print("getting route")

    o <- input$origin
    w <- input$waypoint
    q <- input$waypoint2
    d <- input$destination

    res <- google_directions(key = api_key,
                             origin = o,
                             waypoints = list(stop = w,
                                              stop = q),
                             destination = d,
                             optimise_waypoints = TRUE,
                             mode = "driving")

    df_route <- data.frame(route = res$routes$overview_polyline$points)

    df_way <- cbind(
      res$routes$legs[[1]]$end_location,
      data.frame(address = res$routes$legs[[1]]$end_address)
    )

    df_way$order <- as.character(1:nrow(df_way))

    google_map_update(map_id = "mapWarsaw") %>%
      clear_traffic() %>%
      clear_polylines() %>%
      clear_markers() %>%
      add_traffic() %>%
      add_polylines(data = df_route,
                    polyline = "route",
                    stroke_colour = "#FF33D6",
                    stroke_weight = 7,
                    stroke_opacity = 0.7,
                    info_window = "New route",
                    load_interval = 100) %>%
      add_markers(data = df_way,
                  info_window = "end_address",
                  label = "order")
  })
}


shinyApp(ui, server)

【讨论】:

  • @user9534023 - 如果这回答了您的问题,您可以按投票箭头旁边的灰色“勾号”将其设为“已接受”并关闭它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-12
  • 1970-01-01
相关资源
最近更新 更多