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
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:
You can use my
googleway
package to both get the directions/routes, and plot it on a Google mapTo 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
And similarly in a Shiny app
I use "for loop" to solve such problem,just draw polylines one by one. (sorry for my Chinese expression ^_^) for examply :
or like a more complex one