Setting whole dataframe as waypoints in google maps in R
Clash Royale CLAN TAG#URR8PPP
Setting whole dataframe as waypoints in google maps in R
I am plotting Google maps polyline maps in my r shiny dashboard. I have my start point and destination point, All points i.e lattitude and longitude in a DataFrame called mydf
.
I want map to take 1 row as my origin and last row of mydf as destination with all other points as waypoints to it
mydf
Here is my sample code which i have tried experimenting it:
library(shinydashboard)
library(googleway)
server <- function(input, output) {
#google_Map API Key
key <-"MY_API_KEY"
#calling google keys function
google_keys()
mydf <<- data.frame(to_lat = c(12.915383,12.916363,12.916805,12.917600,12.916962,12.916403,12.923625,12.931930,12.937179),
to_long = c( 77.632263, 77.632260,77.628373,77.624757,77.621871,77.620111,77.618622,77.622677,77.626762))
n <- nrow(mydf)
datar<<- function()
#This code gets specified points as way points I need all points as waypoints
df1<- google_directions(origin = unlist(mydf[1,1:2]),destination =unlist(mydf[n,1:2]),waypoints =list(unlist(mydf[,1:2])), mode = "driving", key = key, simplify = TRUE)
d <-df1
#to store the series of coordinates as a single string
pl <- direction_polyline(df1)
#convert it to dataframe
df <- data.frame(polyline = pl)
#pusihng it to map output
output$map <- renderGoogle_map(
#map function to display map to your application
google_map(key = key)%>%
#addind line with weigth and data from the frame created
add_polylines(data = df, polyline = "polyline", stroke_weight = 3)
)
datar()
shinyApp(ui, server)
Note:This code works fine for selected way points .i.e waypoints =list(unlist(mydf[2,1:2]),unlist(mydf[5,1:2]))
waypoints =list(unlist(mydf[2,1:2]),unlist(mydf[5,1:2]))
Following is the error when above code is executed:
Error in paste0(type, " must be either a numeric vector of lat/lon
coordinates, or an address string") : argument "type" is missing,
with no default
How can I solve this?
google_directions()
shiny
@SymbolixAU the above sample code works very fine. It takes the data and even plot the map successfully as per the
google_directions()
API . I just need to send a list of data points as input data to the map. Assume i have 10 lattitue and longitude points . I want all the 10 points as my waypoints
value.– Rahul R K
Aug 6 at 7:44
google_directions()
waypoints
The examples in
?google_directions
shows you a few ways to pass waypoints into the functions. Do these work for you?– SymbolixAU
Aug 6 at 9:43
?google_directions
I'm sorry but I don't understand your issue. If the code in your question "works very fine", then which part doesn't work? In your question you should only include an example data set, and the function call which is causing the issue.
– SymbolixAU
Aug 6 at 9:49
Here's what you should do. Create a dummy/example data.frame of 5 lon/lat pairs. Edit your question with this example data.frame. Delete all the code you currently have in the question, and replace it with a single call to
google_directions
, including your attempt to include the waypoints.– SymbolixAU
Aug 6 at 10:10
google_directions
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Your issue is specific to the
google_directions()
call; there is something wrong with your input data. You can/should simplify this question by removing all theshiny
code, and supplying an example data set we can use to recreate your question.– SymbolixAU
Aug 6 at 7:34