在R中使用传单绘制旅程路径 [英] Drawing journey path using leaflet in R

查看:83
本文介绍了在R中使用传单绘制旅程路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个Shiny仪表板,其中使用leaflet packageR中绘制的起始经度/纬度和终止经度/纬度与dataframe对应

I am creating a Shiny dashboard with a dataframe of start longitude/latitude and end longitude/latitude cooridnated that I have plotted in R using the leaflet package:

`m=leaflet()%>%
      addTiles() %>%
      addMarkers(lng=(data$Start_long[i:j]), lat=(data$Start_lat[i:j]),popup="Start") %>%
      addCircleMarkers(lng=(data$End_long[i:j]), lat=(data$End_lat[i:j]),popup="End",clusterOptions=markerClusterOptions())`

我想知道是否有一种方法可以通过公共交通路线来协调起点和终点(也许通过google maps API或馆内函数,或者失败了,可以通过直线联接坐标?

I was wondering if there was a way to join the start and end coordinated by public transport routes (maybe through google maps API or in-library functions or failing that, join the coordinates by a straight line?

推荐答案

您可以使用我的googleway包来获取路线/路线,并将其绘制在Google地图上

You can use my googleway package to both get the directions/routes, and plot it on a Google map

要使用Google的API,您需要为每个要使用的API提供有效的密钥.在这种情况下,您需要一个方向键,并绘制地图. '需要一个地图javascript键

To use Google's API you need a valid key for each API you want to use. In this case you'll want a directions key, and for plotting the map you'll want a maps javascript key

(您可以生成一个密钥,并根据需要为两个API启用它)

(You can generate one key and enable it for both APIs if you wish)

要调用Directions API并将其绘制在R中,您可以

To call the Directions API and plot it in R, you can do

library(googleway)

api_key <- "your_directions_api_key"
map_key <- "your_maps_api_key"

## set up a data.frame of locations
## can also use 'lat/lon' coordinates as the origin/destination
df_locations <- data.frame(
  origin = c("Melbourne, Australia", "Sydney, Australia")
  , destination = c("Sydney, Australia", "Brisbane, Australia")
  , stringsAsFactors = F
)

## loop over each pair of locations, and extract the polyline from the result
lst_directions <- apply(df_locations, 1, function(x){
  res <- google_directions(
    key = api_key
    , origin = x[['origin']]
    , destination = x[['destination']]
  )

  df_result <- data.frame(
    origin = x[['origin']]
    , destination = x[['destination']]
    , route = res$routes$overview_polyline$points
  )
  return(df_result)
})

## convert the results to a data.frame
df_directions <- do.call(rbind, lst_directions)

## plot the map
google_map(key = map_key ) %>%
  add_polylines(data = df_directions, polyline = "route")

同样在闪亮"应用中

library(shiny)
library(shinydashboard)
library(googleway)

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    textInput(inputId = "origin", label = "Origin"),
    textInput(inputId = "destination", label = "Destination"),
    actionButton(inputId = "getRoute", label = "Get Rotue"),
    google_mapOutput("myMap")
  )
)

server <- function(input, output){

  api_key <- "your_directions_api_key"
  map_key <- "your_maps_api_key"

  df_route <- eventReactive(input$getRoute,{

    print("getting route")

    o <- input$origin
    d <- input$destination

    return(data.frame(origin = o, destination = d, stringsAsFactors = F))
  })


  output$myMap <- renderGoogle_map({

    df <- df_route()
    print(df)
    if(df$origin == "" | df$destination == "")
      return()

    res <- google_directions(
      key = api_key
      , origin = df$origin
      , destination = df$destination
    )

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

    google_map(key = map_key ) %>%
      add_polylines(data = df_route, polyline = "route")
  })
}

shinyApp(ui, server)

这篇关于在R中使用传单绘制旅程路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆