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

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

问题描述

我正在创建一个 Shiny 仪表板,其中包含我在 R 中绘制的起始经度/纬度和结束经度/纬度的 dataframe使用传单包:

`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())`

我想知道是否有办法连接公共交通路线协调的起点和终点(可能通过谷歌地图 API 或图书馆内的功能,或者如果失败,则通过直线连接坐标?

解决方案

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

要使用 Google 的 API,您需要为要使用的每个 API 提供一个有效密钥.在这种情况下,您需要一个

<小时>

在闪亮的应用程序中也是如此

库(闪亮)图书馆(闪亮的仪表板)图书馆(谷歌路)ui <-仪表板页面(仪表板标题(),仪表板边栏(),仪表板主体(textInput(inputId = "origin", label = "Origin"),textInput(inputId = "目的地", label = "目的地"),actionButton(inputId = "getRoute", label = "Get Rotue"),google_mapOutput("我的地图")))服务器 <- 功能(输入,输出){api_key <- "your_directions_api_key"map_key <- "your_maps_api_key"df_route <- eventReactive(input$getRoute,{print("获取路线")o <- 输入$原点d <- 输入$目的地return(data.frame(origin = o,destination = d,stringsAsFactors = F))})output$myMap <- renderGoogle_map({df <- df_route()打印(df)if(df$origin == "" | df$destination == "")返回()res <- google_directions(键 = api_key, 原点 = df$原点, 目的地 = df$目的地)df_route <- data.frame(route = res$routes$overview_polyline$points)google_map(key = map_key) %>%add_polylines(数据= df_route,折线=路线")})}闪亮的应用程序(用户界面,服务器)

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())`

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?

解决方案

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

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

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

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")


And similarly in a Shiny app

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天全站免登陆