5

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?

Daniel
  • 1,202
  • 2
  • 16
  • 25
mystery man
  • 417
  • 2
  • 5
  • 15

3 Answers3

11

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

enter image description here


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)

enter image description here

SymbolixAU
  • 25,502
  • 4
  • 67
  • 139
3

You can addPolylines() to the map.

It takes two vectors as arguments, one for the lat and one for the lng, where each row is a 'waypoint'.
It's difficult to help you without knowing the structure of your data. MRE:

library(leaflet)
cities <- read.csv(textConnection("
City,Lat,Long,Pop
Boston,42.3601,-71.0589,645966
Hartford,41.7627,-72.6743,125017
New York City,40.7127,-74.0059,8406000
Philadelphia,39.9500,-75.1667,1553000
Pittsburgh,40.4397,-79.9764,305841
Providence,41.8236,-71.4222,177994
"))

leaflet() %>% 
    addTiles() %>% 
    addPolylines(lat = cities$Lat, lng = cities$Long)
GGamba
  • 13,140
  • 3
  • 38
  • 47
  • i tried Polylines however all the lines were joined. The data is pretty much 5 columns, name of the place, and the stand/end long and lats – mystery man Feb 03 '17 at 14:55
  • It really depends on how your data is structured. Pls provide a reproducible example. – GGamba Feb 03 '17 at 14:58
  • Hope this helps: Start End Date Startlat Startlon Endlat Endlon Harwood Bury 17/05/16 53.5984 -2.38731 53.59125 -2.29713 Harwood Wigan 17/05 /16 53.5984 -2.38731 53.54582 -2.63202 etc etc – mystery man Feb 03 '17 at 15:28
  • sorry cant seem to get the data into a table `Start End Date Startlat Startlon Endlat Endlon Harwood Bury 17/05/16 53.5984 -2.38731 53.59125 -2.29713 Harwood Wigan 17/05 /16 53.5984 -2.38731 53.54582 -2.63202 etc etc` – mystery man Feb 03 '17 at 15:34
  • @mysteryman - edit your question and put the data in there. use `dput(head(yourData))`, as that will give the correct structure of the data for others to use – SymbolixAU Feb 03 '17 at 22:30
0

I use "for loop" to solve such problem,just draw polylines one by one. (sorry for my Chinese expression ^_^) for examply :

for(i in 1:nrow(sz)){
     if(i<=nrow(sz) ){
      a <- as.numeric(c(sz[i,c(8,10)])); 
      b <- as.numeric(c(sz[i,c(9,11)]));
      A <- A %>% addPolylines(a,b,group=NULL,weight = 1,color = "brown",
                              stroke = TRUE,fill = NULL,opacity = 0.8)}

or like a more complex one

for(j in 0:23){if(j<=23)
  #j--切每小时数据
  j1 <- as.character(paste(j,"点",sep='')) 
sz <- sz121[sz121$h==j,]
sz_4 <- sz121[sz121$bi_state==4 &sz121$h==j ,]
sz_8 <- sz121[sz121$bi_state==8&sz121$h==j,]
#还原A
A <- leaflet(sz121) %>% amap() %>% addLabelOnlyMarkers(~s_lon,~s_lat) %>% 
  addLegend(title=j1,colors=NULL,labels =NULL,position="topleft")
A <- A %>%addCircleMarkers(data=sz_8,~s_lon,~s_lat,color="orange",fill=TRUE,fillColor = "red", opacity = 1,fillOpacity=0.8,
                           weight =1,radius = 10) %>%addCircleMarkers(data=sz_4,~s_lon,~s_lat,color="black",fill=TRUE,fillColor = "red", 
                                                                      opacity = 1,fillOpacity=0.8,weight =5,radius = 10 ) %>% 
  addCircleMarkers(data=sz_8,~e_lon,~e_lat,color="orange",fill=TRUE,fillColor = "blue", opacity = 1,fillOpacity=0.8,weight=1,radius = 10) %>%
  addCircleMarkers(data=sz_4,~e_lon,~e_lat,color="black",fill=TRUE,fillColor = "blue", opacity = 1,fillOpacity=0.8,weight =5,radius = 10 ) 
for(i in 1:nrow(sz)){
  #i--画路径
  if(i<=nrow(sz) ){
    a <- as.numeric(c(sz[i,c(8,10)]));
    b <- as.numeric(c(sz[i,c(9,11)]));
    A <- A %>% addPolylines(a,b,group=NULL,weight = 1,color = "brown",stroke = TRUE,fill = NULL,opacity = 0.8)
  }
  if(i==nrow(sz)){print(A)}
}
Sys.sleep(3)
}
罗梅菲
  • 1
  • 1