last day (18 days later) » 

21:51
0
Q: How to make route in two point in googleway

AntonioI'm not able to generate the map, could someone help me? The graph I want to generate is the same as the one answered in this question: Googleway R Alternative routes I inserted the API Key and when run the code, appears error in df_routes: Error in lst[[c("routes", "overview_polyline", "points")...

what does your df look like?
Thanks for reply! I did a test with yours datas just to see. But my idea is to calculate the distance between two properties of the df dataset that I entered in the question above.
is df a valid response from Google? Can you show me what df looks like when you print it in the console?
Ok, just a moment
I inserted in the question. My idea is to calculate the route between two properties, for example, 1 and 3. And I saw that google_directions does this.
In your original question you create df with this line: df <- google_directions(...). What does this df look like? does the google_directions() funciton return anything and is df valid?
21:51
I inserted the figure in the question above to you see.
you inserted the object df1 (with the 1). I need to see df because the error you're getting is in this line - direction_polyline(df) - so I need to know what df looks like.
So actually like I said in the question, I just tried to reproduce what you did in this question here: stackoverflow.com/questions/49102457/… I copied your answer and I tried to apply, but I couldn't generate the map. I intend to generate the map you made first, if I can, then I try to do it for the df1 database that refers to the properties that I inserted above. Is it more understandable?
yes I know what you're trying to do. However, to generate the map, you need to have the result of the google_directions() function, which you have assigned to df using the code df <- google_directions(...).
If the result of the google_directions() function is not a valid response from Google, then nothing will work.
so I need to know what your df object is.
22:07
First of all, I just wanted to understand one thing. I went to read rdocumentation.org/packages/googleway/versions/2.7.6/topics/…. I took your first example there: test<-google_directions(origin = c(-37.8179746, 144.9668636), destination = c(-37.81659, 144.9841),mode = "driving") However, instead of walking, I put driving, then when I put test, the following appeared:
> test
$geocoded_waypoints
geocoder_status place_id types
1 OK ChIJ0ZeAo7ZC1moRJx-s40-E0cU street_address
2 OK ChIJIdtrbupC1moRMPT0CXZWBB0 establishment, point_of_interest, train_station, transit_station

$routes
bounds.northeast.lat bounds.northeast.lng bounds.southwest.lat bounds.southwest.lng copyrights
1 -37.81494 144.9855 -37.81782 144.9674 Map data ©2022 Google
Does that mean it worked or not?
yes that output suggests the example worked.
you can see in the output the $status says "OK"
whereas in the exmaple on your StackOverflow question you see the $status says "ZERO_RESULTS"
Great and what do I do next to generate the map and route?
The rest of the code should work, just replace df with test
```
df_routes <- data.frame(polyline = direction_polyline(test))

google_map() %>%
add_polylines(data = df_routes, polyline = "polyline")
```
I understand now! So if I replace the lat and long from your example to those of the properties I entered in the question, it has to work, right?
yes exactly
but you need to check the status of the result.
i.e., the status of the response from Google
22:18
I will test, one moment
It worked, how great! Thank you very much!

Just two questions: is there a way to calculate the time between the two points and the other is if you can show alternative routes, because it might have more than one path?

I did like this:
test<-google_directions(origin = c(-24.872139, -50.038787), destination = c(-24.9062992895515, -50.0125745903862), mode = "driving")

df_routes <- data.frame(polyline = direction_polyline(test))

google_map() %>%
add_polylines(data = df_routes, polyline = "polyline")
direction_steps(test) shows you the duration and distance of each leg of the journey
direction_steps(test)$duration will specifically show you the time of each leg
And is it possible to show alternative routes on the map? I think I saw it somewhere
yes - when you specify alternatives = TRUE in the google_directions() function, if possible it will return alternative routes.
The rest of the code will plot them without any modification
22:38
Great! just last question, I didn't understand very well direction_steps(test). I would just like the distance in km from one point to another, or maybe meters. Just this information.
direction_steps(test)$distance
gives you the distance of each leg
I did and gave the result:

> direction_steps(test)$distance
text value
1 25 m 25
2 0.9 km 940
3 4.6 km 4584
4 0.5 km 543
5 61 m 61
Can you explain this result to me a little? For example, in the third line we have: 4.6 km in text and 4584 in value. Does that mean the distance between the points is 4.6 km? If yes, what is the 61 m in the last line?
the distance of the entire route is sum(as.numeric(direction_steps(test)$distance$value))
The result test you get from Goolge gives the driving instructions for the entire route. But it's split into legs. In this example there are 5 legs. And each leg has a distance & time.
The official documentation explains in greate detail what Google gives you in their response - developers.google.com/maps/documentation/directions/…
23:00
Understood now. In this case, two routes appeared for me, but it only considers one when doing direction_steps(test)$duration. It is correct this?
that is my understanding
Just to know is considered just a route when I do direction_steps(test)$duration,even if other alternative routes appear, right?
yes, direction_steps(test)$duration is only the duration of the first route in the response, not the alternatives.
Perfect, that's exactly what I wanted to know. Thank you very much, you helped me a lot. My idea is to solve this question here: https://stackoverflow.com/questions/71499596/generate-routes-between-two-points-in-google-maps-on-shiny
I used OpenstreetMap, but the routing issue is not very good, just like Google Maps, I believe that google has more routes.
Actually, that's not correct
you can get the result of both routes:

```
test$routes$legs[[1]]$steps
test$routes$legs[[2]]$steps
```
However, the function direction_steps() is a convenience function I wrote, but it only returns the first route.
but the others are avaialble
For example, the distance of the second route is

```
test$routes$legs[[2]]$steps[[1]]$distance
```
23:10
Oh great! Because if I want to know the distance of other alternative routes, I can show the distance in km of the closest one.
> sum(as.numeric(direction_steps(test)$distance$value))
[1] 6153
> sum(as.numeric(test$routes$legs[[2]]$steps[[1]]$distance$value))
[1] 8070
I can compare this way.
This question: stackoverflow.com/questions/71499596/… I can used your method, right?
yes I think so.
Thanks for all the help! Will you want me to delete this question or improve it so you can answer?
23:25
You're welcome.

It's up to you what you do with the question. You can answer it youself if you want. I probably won't add an answer because I think we've covered everything in this chat and I don't really have time to write it up

  last day (18 days later) »